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 stub File 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.txt txt1.txt /home/hadoop/Data/luceneData/txt2.txt txt2.txt /home/hadoop/Data/luceneData/txt5.txt txt5.txt /home/hadoop/Data/luceneData/txt4.txt txt4.txt /home/hadoop/Data/luceneData/txt3.txt txt3.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 stub System.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 stub File 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 stub StringReader 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 stub StringReader 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 stub BufferedReader 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 args 10: * @throws IOException 11: */ 12: public static void main(String[] args) throws IOException { 13: // TODO Auto-generated method stub 14: 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 args 10: * @throws IOException 11: */ 12: public static void main(String[] args) throws IOException { 13: // TODO Auto-generated method stub 14: 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: }
从标准输入中读取
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 stub BufferedReader 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 stub PrintWriter out =new PrintWriter(System.out,true); out.println("Hello,world!"); } }
Hello,world!