处理流:增强功能、提高效率。处理流一定要在节点流之上
一、缓冲流,在原有的节点流上包上一层缓冲流,[
1、字节缓冲流 :BufferedInputStream、BufferedOutputStream
package com.bjsxt.io.buffered;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 字节流文件拷贝+缓冲流 ,提高性能
* 缓冲流(节点流)
* @author Administrator
*
*/
public class BufferedByteDemo {
/**
* @param args
*/
public static void main(String[] args) {
}
/**
* 文件的拷贝
* @param 源文件路径
* @param 目录文件路径
* @throws FileNotFoundException,IOException
* @return
*/
public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {
//1、建立联系 源(存在且为文件) +目的地(文件可以不存在)
File src =new File(srcPath);
File dest =new File(destPath);
if(! src.isFile()){ //不是文件或者为null
System.out.println("只能拷贝文件");
throw new IOException("只能拷贝文件");
}
//2、选择流
<strong>InputStream is =new BufferedInputStream(new FileInputStream(src));
OutputStream os =new BufferedOutputStream( new FileOutputStream(dest));</strong>
//3、文件拷贝 循环+读取+写出
byte[] flush =new byte[1024];
int len =0;
//读取
while(-1!=(len=is.read(flush))){
//写出
os.write(flush, 0, len);
}
os.flush(); //强制刷出
//关闭流
os.close();
is.close();
}
}
2、字符缓冲流 :BufferedReader、BufferedWriter
package com.bjsxt.io.buffered;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 字符缓冲流 +新增方法(不能发生多态)
* @author Administrator
*
*/
public class BufferedCharDemo {
/**
* @param args
*/
public static void main(String[] args) {
//创建源 仅限于 字符的纯文本
File src =new File("E:/xp/test/Demo03.java");
File dest =new File("e:/xp/test/char.txt");
//选择流
BufferedReader reader =null;
BufferedWriter wr =null;
try {
reader =new BufferedReader(new FileReader(src));
wr =new BufferedWriter(new FileWriter(dest));
//读取操作
/*
char[] flush =new char[1024];
int len =0;
while(-1!=(len=reader.read(flush))){
wr.write(flush, 0, len);
}*/
//新增方法的操作
String line =null;
while(null!=(line=reader.readLine())){
wr.write(line);
//wr.append("\r\n");
wr.newLine(); //换行符号
}
wr.flush();//强制刷出
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("源文件不存在");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取失败");
}finally{
try {
if (null != wr) {
wr.close();
}
} catch (Exception e2) {
}
try {
if (null != reader) {
reader.close();
}
} catch (Exception e2) {
}
}
}
}
二、转换流:字节流转为字符流,主要是处理乱码(编码集、解码集)
1、编码与解码概念
- 编码:字符 -----编码字符集----> 二进制
- 解码:从二进制 ----解码字符集---> 到字符
2、乱码的原因
- 编码与解码的字符集不统一
- 字节数长度不够,字节缺少
package com.bjsxt.io.convert;
import java.io.UnsupportedEncodingException;
public class ConverDemo01 {
/**
* @param args
* @throws UnsupportedEncodingException
*/
public static void main(String[] args) throws UnsupportedEncodingException {
String str ="中国";
byte[] data =str.getBytes();
//字节数不完整
System.out.println(new String(data,0,3));
}
/**
* 编码与解码字符集必须相同,否则乱码
* @throws UnsupportedEncodingException
*/
public static void test1() throws UnsupportedEncodingException{
//解码 byte -->char
String str ="中国"; //gbk
//编码 char -->byte
byte[] data =str.getBytes();
//编码与解码字符集同一
System.out.println(new String(data));
data =str.getBytes("utf-8"); //设定编码字符集
//不同一出现乱码
System.out.println(new String(data));
//编码
byte[] data2 = "中国".getBytes("utf-8");
//解码
str=new String(data2,"utf-8");
System.out.println(str);
}
}