出处: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.没有加换行。
- package com.jamin;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- public class IOStreamText {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- try {
- InputStream is = new FileInputStream("/home/jamin/a.txt"); // 定义输入流
- byte [] buffer = new byte [3];//定义一个byte接收读出的数据
- int length = 0;//定义每次流读取的最大长度
- FileOutputStream fos = new FileOutputStream("/home/jamin/b.txt");//定义输出流
- System.out.println(length);//打印长度,如果如果需要循环多次,则length为buffer最大长度,如果是循环的最后一次,length<=buffer的长度
- while (-1 != (length = is.read(buffer, 0, 3)) ){
- System.out.println(length);
- fos.write(buffer, 0, length);
- }
- fos.flush();
- fos.close();
- is.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
*忠告:在编写Java IO程序时,好的编程习惯是在向外写入数据时,在调用Close之前最好先flush数据。
后续补上Zip流解压缩的使用~
zip流压缩文件的方法~千言万语都是浮云,留一个傻瓜教程给自己,。
这是最简单的单文件压缩
- public class ZipText {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- File file = new File("/home/jamin/codenum.txt");
- File zipFile = new File("/home/jamin/code.zip");
- try {
- InputStream input = new FileInputStream(file);
- ZipOutputStream zis = null;
- zis = new ZipOutputStream(new FileOutputStream(zipFile));
- zis.putNextEntry(new ZipEntry(file.getName()));
- int tmp = 0;
- while ( (tmp = input.read()) != -1){
- zis.write(tmp);
- }
- input.close();
- zis.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
压缩文件夹的方法 ,遍历文件夹~
- <span style="font-size:13px;"> public static void main(String[] args) {
- // TODO Auto-generated method stub
- File file = new File("/home/jamin/testdir");
- File zipFile = new File("/home/jamin/codenum.zip");
- try {
- InputStream input = null;
- ZipOutputStream zos = null;
- zos = new ZipOutputStream(new FileOutputStream(zipFile));
- if(file.isDirectory()){
- File [] list = file.listFiles();
- for(File f : list){
- input = new FileInputStream(f);
- zos.putNextEntry(new ZipEntry(file.getName() + "/" + f.getName()));
- }
- }else{
- zos.putNextEntry(new ZipEntry(file.getName()));
- }
- int temp = 0;
- while( (temp = input.read()) != -1){
- zos.write(temp);
- }
- zos.close();
- input.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }</span>
解压缩单个文件的过程
- <span style="font-size:13px;">public class unZipText {
- public static void main(String[] args) {
- File file = new File ("/home/wangjieming/testdir/code.zip");
- try {
- ZipFile zipfile = new ZipFile(file);
- File outfile = new File("/home/wangjieming/testdir/code.txt");
- ZipEntry zipentry = zipfile.getEntry("codenum.txt");
- OutputStream fos = new FileOutputStream(outfile);
- InputStream is = zipfile.getInputStream(zipentry);
- int temp = 0;
- while((temp = is.read()) != -1){
- fos.write(temp);
- }
- } catch (ZipException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }</span>