JAVA I/O流 学习

来源:互联网 发布:网络规划与设计 编辑:程序博客网 时间:2024/05/23 05:07

出处:http://blog.csdn.net/jamin0107/article/details/6668317

JAVA I/O流 

1.按方向分

输入流:InputStream          FileInputStream           Reader             FileReader

输出流:OutputStream       FileOutputStream         Writer              FileWriter

2.按最小单位分

字节流:InputStream         FileInputStream      OutputStream         FileOutputStream

字符流:Reader                 FileReader                 Writer                      FileWriter

3.按层次分

节点流:   InputStream          FileInputStream             OutputStream               FileOutputStream         

                 Reader                       FileReader                       Writer                        FileWriter

处理流:BufferedInputStream        BufferedOutputStream         BufferedReader              BufferedWriter



流的操作步骤(读/写)

Open a stream

While more information

Read information

Close the stream


字节流的字符编码:
  字符编码把字符转换成数字存储到计算机中,按ASCii 将字母映射为整数。
  把数字从计算机转换成相应的字符的过程称为解码。
  乱码的根源在于编解码方式不统一。在世界上任何一种编码方式中都会向上兼容ASCII码。所以英文没有乱码。
  编码方式的分类:
  ASCII(数字、英文):1 个字符占一个字节(所有的编码集都兼容ASCII)
  ISO8859-1(欧洲):1 个字符占一个字节
  GB-2312/GBK:1 个字符占两个字节。GB代表国家标准。
  GBK是在GB-2312上增加的一类新的编码方式,也是现在最常用的汉字编码方式。
  Unicode: 1 个字符占两个字节(网络传输速度慢)
  UTF-8:变长字节,对于英文一个字节,汉字三个字节。
  原则:保证编解码方式的统一,才能不至于出现错误。   
  I/O学习种常范的两个错误 1。忘了加flush2.没有加换行。


[java] view plaincopy
  1. package com.jamin;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8.   
  9. public class IOStreamText {  
  10.   
  11.   
  12.     /** 
  13.      * @param args 
  14.      */  
  15.     public static void main(String[] args) {  
  16.         // TODO Auto-generated method stub  
  17.   
  18.         try {  
  19.             InputStream is = new FileInputStream("/home/jamin/a.txt");   // 定义输入流  
  20.    
  21.             byte [] buffer = new byte [3];//定义一个byte接收读出的数据  
  22.   
  23.             int length = 0;//定义每次流读取的最大长度   
  24.   
  25.             FileOutputStream fos = new FileOutputStream("/home/jamin/b.txt");//定义输出流  
  26.       
  27.             System.out.println(length);//打印长度,如果如果需要循环多次,则length为buffer最大长度,如果是循环的最后一次,length<=buffer的长度  
  28.              
  29.             while (-1 != (length = is.read(buffer, 03))  ){  
  30.   
  31.   
  32.                 System.out.println(length);  
  33.   
  34.                 fos.write(buffer, 0, length);  
  35.   
  36.             }  
  37.   
  38.             fos.flush();  
  39.   
  40.             fos.close();  
  41.   
  42.             is.close();  
  43.   
  44.   
  45.   
  46.         } catch (FileNotFoundException e) {  
  47.             // TODO Auto-generated catch block  
  48.             e.printStackTrace();  
  49.         }catch (IOException e) {  
  50.             // TODO Auto-generated catch block  
  51.             e.printStackTrace();  
  52.         }  
  53.     }  
  54.   
  55. }  



*忠告:在编写Java IO程序时,好的编程习惯是在向外写入数据时,在调用Close之前最好先flush数据。

后续补上Zip流解压缩的使用~

zip流压缩文件的方法~千言万语都是浮云,留一个傻瓜教程给自己,。

这是最简单的单文件压缩

[java] view plaincopy
  1. public class ZipText {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         File file = new File("/home/jamin/codenum.txt");  
  9.         File zipFile = new File("/home/jamin/code.zip");  
  10.         try {  
  11.             InputStream input = new FileInputStream(file);  
  12.             ZipOutputStream zis = null;  
  13.             zis = new ZipOutputStream(new FileOutputStream(zipFile));  
  14.             zis.putNextEntry(new ZipEntry(file.getName()));  
  15.             int tmp = 0;  
  16.             while ( (tmp = input.read()) != -1){  
  17.                 zis.write(tmp);  
  18.             }  
  19.             input.close();  
  20.             zis.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.     }  
  30.   
  31. }  

压缩文件夹的方法,遍历文件夹~

[java] view plaincopy
  1. <span style="font-size:13px;">    public static void main(String[] args) {  
  2.         // TODO Auto-generated method stub  
  3.         File file = new File("/home/jamin/testdir");  
  4.         File zipFile = new File("/home/jamin/codenum.zip");  
  5.         try {  
  6.             InputStream input = null;  
  7.             ZipOutputStream zos = null;  
  8.             zos = new ZipOutputStream(new FileOutputStream(zipFile));  
  9.             if(file.isDirectory()){  
  10.                 File [] list = file.listFiles();  
  11.                 for(File f : list){  
  12.                     input = new FileInputStream(f);  
  13.                     zos.putNextEntry(new ZipEntry(file.getName() + "/" + f.getName()));  
  14.                       
  15.                 }  
  16.   
  17.             }else{  
  18.                 zos.putNextEntry(new ZipEntry(file.getName()));  
  19.             }  
  20.             int temp = 0;  
  21.   
  22.             while( (temp = input.read()) != -1){  
  23.                 zos.write(temp);  
  24.             }  
  25.   
  26.             zos.close();  
  27.             input.close();  
  28.   
  29.         } catch (FileNotFoundException e) {  
  30.             // TODO Auto-generated catch block  
  31.             e.printStackTrace();  
  32.         } catch (IOException e) {  
  33.             // TODO Auto-generated catch block  
  34.             e.printStackTrace();  
  35.         }  
  36.   
  37.     }  
  38.   
  39. }</span>  


解压缩单个文件的过程


[java] view plaincopy
  1. <span style="font-size:13px;">public class unZipText {  
  2.   
  3.       
  4.       
  5.     public static void main(String[] args) {  
  6.         File file = new File ("/home/wangjieming/testdir/code.zip");  
  7.         try {  
  8.             ZipFile zipfile = new ZipFile(file);  
  9.             File outfile = new File("/home/wangjieming/testdir/code.txt");  
  10.               
  11.             ZipEntry zipentry = zipfile.getEntry("codenum.txt");  
  12.               
  13.             OutputStream fos = new FileOutputStream(outfile);  
  14.               
  15.             InputStream is =  zipfile.getInputStream(zipentry);  
  16.               
  17.             int temp = 0;  
  18.               
  19.             while((temp = is.read()) != -1){  
  20.                   
  21.                 fos.write(temp);      
  22.             }  
  23.               
  24.               
  25.         } catch (ZipException 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. }</span>