1,File類
既能代表一個特定檔案的名稱,又能代表一個目錄下的一組檔案的名稱。如果它指向的是一個檔案集,可以對詞集合調用list()方法,返回一個字元數組。
例1
package FileTest;import java.io.File;public class DirList {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFile filedir=new File("/home/hadoop/Data/luceneData");File[] textFiles=filedir.listFiles();for(File file :textFiles){System.out.println(file.getAbsolutePath());System.out.println(file.getName());}}}結果
/home/hadoop/Data/luceneData/txt1.txttxt1.txt/home/hadoop/Data/luceneData/txt2.txttxt2.txt/home/hadoop/Data/luceneData/txt5.txttxt5.txt/home/hadoop/Data/luceneData/txt4.txttxt4.txt/home/hadoop/Data/luceneData/txt3.txttxt3.txt
輸出檔案的絕對路徑和檔案名稱
2,輸入和輸出
緩衝輸入檔案
package InOutPut;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOError;import java.io.IOException;public class BufferedInputFile {/** * @param args */public static String read(String filename) throws IOException{BufferedReader in=new BufferedReader(new FileReader(filename));String s;StringBuilder sb =new StringBuilder();while((s=in.readLine())!=null){sb.append(s+"\n");}in.close();return sb.toString();}public static void main(String[] args) throws IOException {// TODO Auto-generated method stubSystem.out.println(read("./src/FileTest/DirList.java"));}}
結果
package FileTest;import java.io.File;import java.io.IOException;public class DirList {/** * @param args * @throws IOException */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubFile filedir=new File("/home/hadoop/Data/luceneData");File[] textFiles=filedir.listFiles();for(File file :textFiles){System.out.println(file.getAbsolutePath());System.out.println(file.getCanonicalPath());System.out.println(file.getName());}}}
從記憶體輸入
package InOutPut;import java.io.IOException;import java.io.StringReader;public class MemoryInput {/** * @param args * @throws IOException */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubStringReader inReader=new StringReader(BufferedInputFile.read("./src/FileTest/DirList.java"));int c;while((c=inReader.read())!=-1){System.out.println((char)c);}System.out.println(BufferedInputFile.read("./src/FileTest/DirList.java"));}}
結果
package InOutPut;import java.io.IOException;import java.io.StringReader;public class MemoryInput {/** * @param args * @throws IOException */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubStringReader inReader=new StringReader(BufferedInputFile.read("./src/FileTest/DirList.java"));int c;while((c=inReader.read())!=-1){System.out.println((char)c);}System.out.println(BufferedInputFile.read("./src/FileTest/DirList.java"));}}
基本的檔案輸出
package InOutPut;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.io.PrintWriter;import java.io.StringReader;public class BasicFileOutput {static String file ="Output.out";/** * @param args */public static void main(String[] args) throws IOException{// TODO Auto-generated method stubBufferedReader in= new BufferedReader(new StringReader(BufferedInputFile.read("./src/FileTest/DirList.java")));PrintWriter out=new PrintWriter(file);int lineCount=1;String s;while((s=in.readLine()) !=null){out.println(lineCount++ + ": "+s);}out.close();System.out.println(BufferedInputFile.read(file));//從記憶體中讀取System.out.println(BufferedInputFile.read("./Output.out"));}}
結果
1: package FileTest;2: 3: import java.io.File;4: import java.io.IOException;5: 6: public class DirList {7: 8: /**9: * @param args10: * @throws IOException 11: */12: public static void main(String[] args) throws IOException {13: // TODO Auto-generated method stub14: File filedir=new File("/home/hadoop/Data/luceneData");15: File[] textFiles=filedir.listFiles();16: for(File file :textFiles){17: System.out.println(file.getAbsolutePath());18: System.out.println(file.getCanonicalPath());19: System.out.println(file.getName());20: }21: 22: }23: 24: }1: package FileTest;2: 3: import java.io.File;4: import java.io.IOException;5: 6: public class DirList {7: 8: /**9: * @param args10: * @throws IOException 11: */12: public static void main(String[] args) throws IOException {13: // TODO Auto-generated method stub14: File filedir=new File("/home/hadoop/Data/luceneData");15: File[] textFiles=filedir.listFiles();16: for(File file :textFiles){17: System.out.println(file.getAbsolutePath());18: System.out.println(file.getCanonicalPath());19: System.out.println(file.getName());20: }21: 22: }23: 24: }
3,I/O
從標準輸入中讀取
package IO;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Echo {/** * @param args * @throws IOException */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubBufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));String s;while((s=stdin.readLine()) != null && s.length() !=0){System.out.println(s);}}}
結果
哈哈哈哈
輸出
package IO;import java.io.PrintWriter;public class ChangeSystemOut {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubPrintWriter out =new PrintWriter(System.out,true);out.println("Hello,world!");}}
結果
Hello,world!