IO流简介
IO流 用于设备间进行数据传输的操作。
IO流可以分为字节流和字符流,也可以分为输入流和输出流。
字符流 = 字节流 + 编码表
InputStreamReader、OutputStreamWriter 转换流
- 把对应的字节流转换字符流使用
- 可以指定码表进行读写文件的数据
字节流与字符流
- 字节流
- 字节输入流
- InputStream 抽象类
- int read() : 一次读取一个字节
- int read(byte[] bys) : 一次读取一个字节数组
- FileInputStream
- BufferedInputStream
- InputStream 抽象类
- 字节输出流
- OutputStream 抽象类
- void write() : 一次写一个字节
- void write(byte[] bys,int index,int len) : 一次写字节数组的一部分
- FileOutputStream
- BufferedOutputStream
- OutputStream 抽象类
- 字节输入流
- 字符流
- 字符输入流
- Reader 抽象类
- int Read() : 一次读取一个字符
- int Read(char[] chars) : 一次读取一个字符数组
- InputStreamReader
- FileReader
- BufferedReader
- String readLine() : 一次读取一行数据
- Reader 抽象类
- 字符输入流
- 字符输出流
- Writer 抽象类
- void write(int ch) : 一次写一个字符
- void write(char[] chars): 一次写一个字符数组
- OutputStreamWriter
- FileWriter
- BufferedWriter
- void newLine(): 写一个换行符
- void write(String line)
- Writer 抽象类

字节流Demo
public class CopyDemo {
public static void main(String[] args) {
long start = System.currentTimeMillis();
try {
method01("d:\\hello.pdf", "e:\\copy.pdf");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("字节流一次读取一个字节,花费时长:" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
try {
method02("d:\\hello.pdf", "e:\\copy.pdf");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("字节流一次读取1024字节数组,花费时长:" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
try {
method03("d:\\hello.pdf", "e:\\copy.pdf");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("高效字节流一次读取一个字节,花费时长:" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
try {
method04("d:\\hello.pdf", "e:\\copy.pdf");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("高效字节流一次读取1024字节数组,花费时长:" + (System.currentTimeMillis() - start));
}
// 基本字节流一次读取一个字节
public static void method01(String srcPath, String destPath) throws IOException {
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destPath);
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fis.close();
fos.close();
}
// 基本字节流一次读取1024字节数组
public static void method02(String srcPath, String destPath) throws IOException {
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destPath);
byte[] bys = new byte[1024];
int len;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fis.close();
fos.close();
}
// 高效字节流一次读取一个字节
public static void method03(String strPath, String destPath) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(strPath));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));
int by;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bis.close();
bos.close();
}
// 高效字节流一次读取1024字节数组
public static void method04(String srcPath, String destPath) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcPath));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bis.close();
bos.close();
}
}
输出结果:
字节流一次读取一个字节,花费时长:4419
字节流一次读取1024字节数组,花费时长:6
高效字节流一次读取一个字节,花费时长:26
高效字节流一次读取1024字节数组,花费时长:3
字符流Demo
public class CopyDemo2 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
try {
method01("d:\\hello.pdf", "e:\\copy.pdf");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("InputStreamReader字符流一次读取一个字符,花费时长:" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
try {
method02("d:\\hello.pdf", "e:\\copy.pdf");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("InputStreamReader字符流一次读取1024字符数据,花费时长:" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
try {
method03("d:\\hello.pdf", "e:\\copy.pdf");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("FileReader字符流一次读取1024字符数据,花费时长:" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
try {
method04("d:\\hello.pdf", "e:\\copy.pdf");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("BufferedReader高效字符流一次读取一行字符数据,花费时长:" + (System.currentTimeMillis() - start));
}
public static void method01(String srcPath, String destPath) throws IOException {
InputStreamReader isR = new InputStreamReader(new FileInputStream(srcPath), "UTF-8");
OutputStreamWriter osW = new OutputStreamWriter(new FileOutputStream(destPath), "UTF-8");
int by;
while ((by = isR.read()) != -1) {
osW.write(by);
}
isR.close();
osW.close();
}
public static void method02(String srcPath, String destPath) throws IOException {
InputStreamReader isR = new InputStreamReader(new FileInputStream(srcPath), "UTF-8");
OutputStreamWriter osW = new OutputStreamWriter(new FileOutputStream(destPath), "UTF-8");
char[] chars = new char[1024];
int len;
while ((len = isR.read(chars)) != -1) {
osW.write(chars, 0, len);
}
isR.close();
osW.close();
}
public static void method03(String srcPath, String destPath) throws IOException {
FileReader fr = new FileReader(srcPath);
FileWriter fw = new FileWriter(destPath);
char[] chars = new char[1024];
int len;
while ((len = fr.read(chars)) != -1) {
fw.write(chars, 0, len);
}
fr.close();
fw.close();
}
public static void method04(String srcPath, String destPath) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(srcPath), "UTF-8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destPath), "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
br.close();
bw.close();
}
}
输出:
InputStreamReader字符流一次读取一个字符,花费时长:293
InputStreamReader字符流一次读取1024字符数据,花费时长:51
FileReader字符流一次读取1024字符数据,花费时长:33
BufferedReader高效字符流一次读取一行字符数据,花费时长:42
网友评论