2.图展示【文本IO/二进制IO】(这是参考自网上的一张总结图,相当经典,方便对比记忆)
3.文本复制的实验Java实现code:
package com.gdufe.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class TestInputOutputStream {
// private final static String SOURCE="t.txt";
// private final static String TARGET="tt.txt";
// private final static String SOURCE="p.png";
// private final static String TARGET="pp.png";
private final static String SOURCE="D:\\Tool_Software\\mysql-installer-community-5.6.23.0.msi";
private final static String TARGET="mysql-installer-community-5.6.23.0.msi";
//D:\Tool_Software\Ryuyan.zip
public static void main(String[] args) {
// TestInputOutputStream.copy1(SOURCE, TARGET);
// TestInputOutputStream.copy2(SOURCE, TARGET);
TestInputOutputStream.copy3(SOURCE, TARGET);
System.out.println("--End---");
}
public static void copy1(String src,String tar){
InputStream input = null;
OutputStream output = null;
try{
input = new FileInputStream(src);
output = new FileOutputStream(tar);
int r;
while((r=input.read())!=-1){
output.write((byte)r);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void copy2(String src,String tar){
InputStream input = null;
OutputStream output = null;
try{
input = new FileInputStream(src);
output = new FileOutputStream(tar);
int length=0;
byte []b = new byte[1024];
while((length=input.read(b))!=-1){
output.write(b,0,length);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void copy3(String src,String tar){
InputStream input = null;
OutputStream output = null;
try{
input = new DataInputStream(new BufferedInputStream(new FileInputStream(src)));
output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(tar)));
/***经过亲测,buffer缓冲流读取时确实更快一些****/
int length=0;
byte []b = new byte[1024]; //先将stream读入字节数组
while((length=input.read(b))!=-1){
output.write(b,0,length);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}