IO-03字节流字符流

来源:互联网 发布:如何禁止卸载软件 编辑:程序博客网 时间:2024/04/29 02:21

在程序中所有的数据都是以流的方式进行传输和保存的,程序需要数据的时候要使用输入流读取数据,而程序需要将一些数据保存起来则需要使用输出流来完成。

在JAVA.IO包中操作文件内容的主要有两大类:字节流,字符流,两类都分为输入和输出操作。在字节流中输出数据主要是使用OutputStream,输入主要使用InputStream,在字符流中输出主要使用Writer类完成,输入只要使用Reader类完成。

主要的操作流程如下:

  • 使用FILE类打开一个文件
  • 通过字节流或者字符流的子类,指定输出的位置。
  • 进行读/写操作
  • 关闭输入/输出

字节流输出操作代码:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package lianxijihe;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.OutputStream;  
  8.   
  9. public class lianxi043 {  
  10.     public static void main(String[] args){  
  11.         //1.使用File类找到一个文件  
  12.         File f = new File("F:\\aaa.txt");  
  13.         //2.通过子类实例化父类  
  14.         try {  
  15.             OutputStream out = new FileOutputStream(f);  
  16.             //3.进行读写操作  
  17.             String ss =  "hello";  
  18.             byte[] b = ss.getBytes();  
  19.             out.write(b);  
  20.             //关闭输出流  
  21.             out.close();  
  22.               
  23.         } catch (FileNotFoundException e) {  
  24.             // TODO Auto-generated catch block  
  25.             e.printStackTrace();  
  26.         } catch (IOException e) {  
  27.             // TODO Auto-generated catch block  
  28.             e.printStackTrace();  
  29.         }  
  30.           
  31.           
  32.     }  
  33. }  
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package lianxijihe;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.OutputStream;  
  8.   
  9. public class lianxi043 {  
  10.     public static void main(String[] args){  
  11.         //1.使用File类找到一个文件  
  12.         File f = new File("F:\\aaa.txt");  
  13.         //2.通过子类实例化父类  
  14.         try {  
  15.             OutputStream out = new FileOutputStream(f);  
  16.             //3.进行读写操作  
  17.             String ss =  "hello1111";  
  18.             byte[] b = ss.getBytes();  
  19.             for(int i=0;i<b.length;i++){//采用循环写入,每次只写入1位  
  20.                 out.write(b[i]);  
  21.             }  
  22.             //关闭输出流  
  23.             out.close();  
  24.               
  25.         } catch (FileNotFoundException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.         } catch (IOException e) {  
  29.             // TODO Auto-generated catch block  
  30.             e.printStackTrace();  
  31.         }  
  32.           
  33.           
  34.     }  
  35. }  

如果不想覆盖之前文件,需要执行追加操作:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package lianxijihe;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.OutputStream;  
  8.   
  9. public class lianxi043 {  
  10.     public static void main(String[] args){  
  11.         //1.使用File类找到一个文件  
  12.         File f = new File("F:\\aaa.txt");  
  13.         //2.通过子类实例化父类  
  14.         try {  
  15.             OutputStream out = new FileOutputStream(f, true);  
  16.             //3.进行读写操作  
  17.             String ss =  "hello1111";  
  18.             byte[] b = ss.getBytes();  
  19.             for(int i=0;i<b.length;i++){//采用循环写入,每次只写入1位  
  20.                 out.write(b[i]);  
  21.             }  
  22.             //关闭输出流  
  23.             out.close();  
  24.               
  25.         } catch (FileNotFoundException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.         } catch (IOException e) {  
  29.             // TODO Auto-generated catch block  
  30.             e.printStackTrace();  
  31.         }  
  32.           
  33.           
  34.     }  
  35. }  
如果想在末尾换行需要加入“\r\n”
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package lianxijihe;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.OutputStream;  
  8.   
  9. public class lianxi043 {  
  10.     public static void main(String[] args){  
  11.         //1.使用File类找到一个文件  
  12.         File f = new File("F:\\aaa.txt");  
  13.         //2.通过子类实例化父类  
  14.         try {  
  15.             OutputStream out = new FileOutputStream(f, true);  
  16.             //3.进行读写操作  
  17.             String ss =  "\r\nhello1111";  
  18.             byte[] b = ss.getBytes();  
  19.             for(int i=0;i<b.length;i++){//采用循环写入,每次只写入1位  
  20.                 out.write(b[i]);  
  21.             }  
  22.             //关闭输出流  
  23.             out.close();  
  24.               
  25.         } catch (FileNotFoundException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.         } catch (IOException e) {  
  29.             // TODO Auto-generated catch block  
  30.             e.printStackTrace();  
  31.         }  
  32.           
  33.           
  34.     }  
  35. }  

字节流输入操作代码:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package lianxijihe;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8.   
  9. public class lianxi044 {  
  10.     public static void main(String[] args){  
  11.         File f = new File("f:\\abc.txt");  
  12.         try {  
  13.             InputStream in = new FileInputStream(f);  
  14.             byte[] b = new byte[(int)f.length()];  
  15.             in.read(b);  
  16.             System.out.println(new String(b));  
  17.             in.close();  
  18.         } catch (FileNotFoundException e) {  
  19.             // TODO Auto-generated catch block  
  20.             e.printStackTrace();  
  21.         } catch (IOException e) {  
  22.             // TODO Auto-generated catch block  
  23.             e.printStackTrace();  
  24.         }  
  25.     }  
  26. }  

一位一位读进来
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package lianxijihe;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8.   
  9. public class lianxi044 {  
  10.     public static void main(String[] args){  
  11.         File f = new File("f:\\abc.txt");  
  12.         try {  
  13.             InputStream in = new FileInputStream(f);  
  14.             byte[] b = new byte[(int)f.length()];  
  15.             for(int i=0;i<b.length;i++){  
  16.                 b[i] =(byte)in.read();  
  17.             }  
  18.               
  19.             System.out.println(new String(b));  
  20.             in.close();  
  21.         } catch (FileNotFoundException e) {  
  22.             // TODO Auto-generated catch block  
  23.             e.printStackTrace();  
  24.         } catch (IOException e) {  
  25.             // TODO Auto-generated catch block  
  26.             e.printStackTrace();  
  27.         }  
  28.     }  
  29. }  

如果不知道文件的大小则:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package lianxijihe;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8.   
  9. public class lianxi044 {  
  10.     public static void main(String[] args){  
  11.         File f = new File("f:\\abc.txt");  
  12.         try {  
  13.             InputStream in = new FileInputStream(f);  
  14.             byte[] b = new byte[1024];  
  15.             int len =0;  
  16.             int tem=0;  
  17.             while((tem = in.read())!=-1){  
  18.                 b[len] = (byte)tem;  
  19.                 len++;  
  20.             }  
  21.           
  22.               
  23.             System.out.println(new String(b,0,len));  
  24.             in.close();  
  25.         } catch (FileNotFoundException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.         } catch (IOException e) {  
  29.             // TODO Auto-generated catch block  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33. }  


字符流输出:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package lianxijihe;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileWriter;  
  5. import java.io.IOException;  
  6. import java.io.Writer;  
  7.   
  8. public class lianxi045 {  
  9.   
  10.     /** 
  11.      * @param args 
  12.      */  
  13.     public static void main(String[] args) {  
  14.         File f = new File("f:\\abc.txt");  
  15.         try {  
  16.             Writer  w = new FileWriter(f);  
  17.             String ss = "aaaaaaaaaaaaa";  
  18.             w.write(ss);  
  19.             w.close();  
  20.         } catch (IOException e) {  
  21.             // TODO Auto-generated catch block  
  22.             e.printStackTrace();  
  23.         }  
  24.           
  25.     }  
  26.   
  27. }  

字符流读取数据:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package lianxijihe;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6. import java.io.Reader;  
  7.   
  8. public class lianxi045 {  
  9.   
  10.     /** 
  11.      * @param args 
  12.      */  
  13.     public static void main(String[] args) {  
  14.         File f = new File("f:\\abc.txt");  
  15.         try {  
  16.             Reader r = new FileReader(f);  
  17.             char[] c = new char[1024];  
  18.             r.read(c);  
  19.             System.out.println(new String(c));  
  20.         } catch (IOException e) {  
  21.             // TODO Auto-generated catch block  
  22.             e.printStackTrace();  
  23.         }  
  24.           
  25.     }  
  26.   
  27. }  

字节流与字符流的区别:
字节流在操作的时候本身不会用到缓冲区,是与本件本身直接操作的,而字符流在操作的时候是使用到缓冲区的。
在所有的硬盘上保存的文件或者是进行传输的时候是以字节的方式进行的。包括图片也是按字节完成的,而字符只有在内存中才会形成,使用字节的操作是最多的。


复制文件的小程序:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package lianxijihe;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.OutputStream;  
  10.   
  11. public class lianxi046 {  
  12.     public static void main(String[] args){  
  13.         if(args.length!=2){  
  14.             System.out.println("输入的格式不对");  
  15.             System.exit(1);  
  16.         }  
  17.         File f1 = new File(args[0]);  
  18.         File f2 = new File(args[1]);  
  19.         if(!f1.exists()){  
  20.             System.out.println("源文件不存在");  
  21.             System.exit(1);  
  22.               
  23.         }  
  24.         try {  
  25.             InputStream in = new FileInputStream(f1);  
  26.             OutputStream out = new FileOutputStream(f2);  
  27.             int temp =0;  
  28.             while((temp=in.read())!=-1){  
  29.                 out.write(temp);  
  30.             }  
  31.             in.close();  
  32.             out.close();  
  33.               
  34.         } catch (FileNotFoundException e) {  
  35.             // TODO Auto-generated catch block  
  36.             e.printStackTrace();  
  37.         } catch (IOException e) {  
  38.             // TODO Auto-generated catch block  
  39.             e.printStackTrace();  
  40.         }  
  41.           
  42.     }  
  43. }  
0 0