JavaIO复习和目录文件的复制

最近用到一些java.io包中的一些类接口,所以写了一些东西来复习。

Input和Output是同一类,而Reader和Writer另属同一类

Reader支持16位的Unicode字符的输出,而InputStream只支持8位字符输出。他们的大概结构如下:

 InputStream的子类有:

               FileInputStream,FilterInputStream,ObjectInputStream,StringBufferInputStream 等

OutputStream的子类有:

               ByteArrayOutputStream,FileOutputStream,FilterOutputStream,PipedOutputStream,ObjectOutputStream。

Reader的子类 有:BufferdReader,InputStreamReader,FilterReader,StringReader,PipedReader,CharArrayReader。

Writer的子类有: BufferedWriter,CharArrayWriter,FilterWriter,OutputStreamWriter,PipedWriter,PrintWriter,StringWriter。

曾遇到一个面试题:

请选择下面的这却答案:

a. System.out 是一个PrintStream。

b. System.out 是一个OutputStream。

c. System.out 是一个FilterOutputStream。

d. System.out 不是一个PrintStream。

e. System.out 在异常时,将抛出IOExcepton。

由于System.out 是一个PrintStream的一个子类,并且PrintStream对象并没有抛出IOException异常。

所以可以看出答案:a b c

例一:InputStream读取文件的应 用:

Java代码
  1. import java.io.FileInputStream;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5.   
  6. <span style="color: rgb(51, 153, 102);">/** 
  7.  *  
  8.  * @author liugao ec06 cumt 
  9.  * 
  10.  */</span>  
  11. public class TestJavaIo {  
  12.   
  13.     <span style="color: rgb(51, 153, 102);">/** 
  14.      * @param args 
  15.      */</span>  
  16.     public static void main(String[] args) {  
  17.         int b=0;  
  18.         long num=0;  
  19.         InputStream in=null;  
  20.         try {  
  21.             in=new FileInputStream("D:/a.txt");  
  22.         } catch (FileNotFoundException e) {  
  23.             System.out.println("文件找不到");  
  24.             System.exit(-1);  
  25.         }  
  26.         try{  
  27.             while((b=in.read()) !=-1){              <span style="color: rgb(51, 153, 102);">//b读取是字符的AI码</span>  
  28.                 System.out.println((char)b);  
  29.                 num++;  
  30.             }  
  31.             in.close();  
  32.             System.out.println();  
  33.             System.out.println("共读取了" + num + " 个字节");  
  34.               
  35.             }catch(IOException e){  
  36.                 System.out.println("文件读取错误");  
  37.                 System.exit(-1);  
  38.             }  
  39.         }  
  40.     }  
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; /** * * @author liugao ec06 cumt * */ public class TestJavaIo { /** * @param args */ public static void main(String[] args) { int b=0; long num=0; InputStream in=null; try { in=new FileInputStream("D:/a.txt"); } catch (FileNotFoundException e) { System.out.println("文件找不到"); System.exit(-1); } try{ while((b=in.read()) !=-1){ //b读取是字符的AI码 System.out.println((char)b); num++; } in.close(); System.out.println(); System.out.println("共读取了" + num + "个字节"); }catch(IOException e){ System.out.println("文件读取错误"); System.exit(-1); } } }

例二:FileReader的应用:

Java代码
  1. import java.io.FileNotFoundException;  
  2. import java.io.FileReader;  
  3. import java.io.IOException;  
  4.   
  5. <span style="color: rgb(51, 153, 102);">/** 
  6.  *  
  7.  * @author ec06cumt 
  8.  * 
  9.  */</span>  
  10. public class TestFileReader {  
  11.   
  12. <span style="color: rgb(51, 153, 102);">    /** 
  13.      * @param args 
  14.      */</span>  
  15.     public static void main(String[] args) {  
  16.         FileReader fr=null;  
  17.         int c=0;  
  18.         int ln=0;  
  19.         try {  
  20.             fr=new FileReader("D:/a.txt");  
  21.             while((c=fr.read())!=-1){  
  22.                 System.out.println((char)c);  
  23.                 ln++;  
  24.             }   
  25.             fr.close();  
  26.               
  27.             <span style="color: rgb(51, 153, 102);">//主要这里的ln的值,当有中文字符时,一个中文字符还是算一个,  
  28.             //但InputStream时就一个中文就两个,由此可以看出Reader和Input的区别< /span>  
  29.             System.out.println("共有:"+ln+"个字符");  
  30.         } catch (FileNotFoundException e) {  
  31.             e.printStackTrace();  
  32.         }catch (IOException e) {  
  33.             e.printStackTrace();  
  34.         }  
  35.     }  
  36.   
  37. }  
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; /** * * @author ec06cumt * */ public class TestFileReader { /** * @param args */ public static void main(String[] args) { FileReader fr=null; int c=0; int ln=0; try { fr=new FileReader("D:/a.txt"); while((c=fr.read())!=-1){ System.out.println((char)c); ln++; } fr.close(); //主要这里的ln的值,当有中文字符时,一个中文字符还是算一个, //但InputStream时就一个中文就两个,由此可以看出Reader和Input的区别 System.out.println("共有:"+ln+"个字符"); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } } }
原文地址:https://www.cnblogs.com/danghuijian/p/4400340.html