首页 > 代码库 > java io流总结
java io流总结
1、java io 字符串 读取、写入一个例子
package sd;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class MyReadFilezifu {
public static void main(String[] args) throws Exception{
File inFile = new File("C:/work/test.txt") ;
File outFile = new File("c:/work/test1.txt") ;
FileReader fr=new FileReader(inFile);
BufferedReader br=new BufferedReader(fr);
FileWriter fw=new FileWriter(outFile) ;
BufferedWriter bw=new BufferedWriter(fw);
while(br.ready()){
bw.write(br.readLine());
}
bw.close() ;
}
}
2、大文件字节流复制
package sd;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MyReadFile {
public static void main(String args[]) throws Exception{
File inFile=new File("E:/src.zip");
File outFile=new File("d:/src2.zip");
FileInputStream fis=new FileInputStream(inFile);
FileOutputStream fos=new FileOutputStream(outFile);
byte[] tmp=new byte[8192];
int length=fis.available()/8192;
for(int i=0;i<length;i++){
fis.read(tmp);
fos.write(tmp);
}
int size=fis.read(tmp);
fos.write(tmp,0,size);
}
}
------------------------------------
字节流都是Stream结尾。字符流都是write或Reader结尾
字符流有缓存区,要close()才能输出。