字节流
输出流 OutputStream -- 写字节,字节数组,字节数组一部分
|-- FileOutputStream 构造方法File,String文件名
|-- BufferedOutputStream 构造方法中,传递任意字节输出流
输入流 InputStream -- 读字节,字节数组,字节数组一部分
|-- FileInputStream 构造方法File,String文件名
|-- BufferedInputStream 构造方法中,传递任意字节输入流
字符流
输出流 Writer -- 写字符,字符数组,字符数组一部分,字符串
|-- OutputStreamWriter 字符转字节桥梁,带编码表名 构造方法中,传递字节输出流
|-- FileWriter 构造方法传递File,String文件名
|-- BufferedWriter 构造方法传递,传递任意字符输出流,特有方法 newLine
输入流 Reader -- 读字符,字符数组
|-- InputStreamReader 字节转字符桥梁,带编码表名 构造方法中,传递字节输入流
|-- FileReader 构造方法传递File,String文件名
|-- BufferedReader 构造方法传递,任意字符输入流,特有方法 readLine() 打印一行
使用技巧 :
数据源:
如果数据源明确就是文本文件,选择FileReader,InputStreamReader 字符输入流读取文件
需要提高效率吗 ,如果需要提高,选择字符数组缓冲,BufferedReader行读取
如果数据源明确不是文本文件,选择FileInputStream 字节输入流读取文件
需要提高效率吗,如果需要提高,选择字节数组缓冲,BufferedInputStream
如果数据源无法明确是什么,选择字节流读取
数据目的:
如果数据目的明确是文本文件,选择FileWriter,OutputStreamWriter 字符输出流写文件
需要提高效率吗,如果需要,选择字符数组缓冲,BufferedWriter写换行
如果数据目的明确不是文本文件,选择FileOutputStream 字节输出流写文件
需要提高效率吗,如果需要提高,选择字节数组缓冲,BufferedOutputStream
如果数据目的无法明确是什么,选择字节流写入
案例:
package cn.itcast.copy;
/*
* 测试文件复制的效率
* 复制一个文件,有4种方式可以选择
* 1. 字节流读取写入单个字节 131635
* 2. 字节流读取写入字节数组 90 5956
* 3. 字节流缓冲区对象读取写入单个字节 1924
* 4. 字节流缓冲区对象读取写入字节数组 85 1257
*
* 将4个方法封装在4个方法中,测试那一个,调用
*/
import java.io.*;
public class Copy {
public static void main(String[] args) {
// 获取开始时间的毫秒值
long start = System.currentTimeMillis();
copy2();
// 获取结束的毫秒值
long end = System.currentTimeMillis();
System.out.println(end - start);
}
//方法4 字节流缓冲区对象读写字节数组
public static void copy4(){
// 创建流对象变量,不要建立对象
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream("c:\\n.iso"));
bos = new BufferedOutputStream(
new FileOutputStream("d:\\n.iso"));
// 利用缓冲区读写字节数组
int len = 0;
byte[] bytes = new byte[1024];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes,0,len);
}
} catch (IOException ex) {
throw new RuntimeException("文件复制失败");
} finally {
try {
if (bis != null)
bis.close();
} catch (IOException ex) {
throw new RuntimeException("释放资源失败");
} finally {
try {
if (bos != null)
bos.close();
} catch (IOException ex) {
throw new RuntimeException("释放资源失败");
}
}
}
}
// 方法3 字节流缓冲区对象读写单个字节
public static void copy3() {
// 创建流对象变量,不要建立对象
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream("c:\\qq.exe"));
bos = new BufferedOutputStream(
new FileOutputStream("d:\\qq.exe"));
// 利用缓冲区读写单个字节
int len = 0;
while ((len = bis.read()) != -1) {
bos.write(len);
}
} catch (IOException ex) {
throw new RuntimeException("文件复制失败");
} finally {
try {
if (bis != null)
bis.close();
} catch (IOException ex) {
throw new RuntimeException("释放资源失败");
} finally {
try {
if (bos != null)
bos.close();
} catch (IOException ex) {
throw new RuntimeException("释放资源失败");
}
}
}
}
// 方法2 字节流读取写入字节数组
public static void copy2() {
// 声明类的变量, = null
// 创建FileInputStream,读取数据源
FileInputStream fis = null;
// 创建FileOutputStream,写数据目的
FileOutputStream fos = null;
try {
// 创建字节输入流,输出流对象
fis = new FileInputStream("c:\\n.iso");
fos = new FileOutputStream("d:\\n.iso");
// 读取一个字节数组,写一个字节数组
int len = 0;
byte[] bytes = new byte[1024];
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
} catch (IOException ex) {
throw new RuntimeException("文件复制失败");
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException ex) {
throw new RuntimeException("关闭资源失败");
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
// 方法1 字节流读取单个字节
public static void copy1() {
// 声明类的变量, = null
// 创建FileInputStream,读取数据源
FileInputStream fis = null;
// 创建FileOutputStream,写数据目的
FileOutputStream fos = null;
try {
// 创建字节输入流,输出流对象
fis = new FileInputStream("c:\\qq.exe");
fos = new FileOutputStream("d:\\qq.exe");
// 读取一个字节,写一个字节
int len = 0;
while ((len = fis.read()) != -1) {
fos.write(len);
}
} catch (IOException ex) {
throw new RuntimeException("文件复制失败");
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException ex) {
throw new RuntimeException("关闭资源失败");
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
}