/**
*创建了一个数组临时存储数据,作为缓冲区
*/
package cn.com.copytxt;
import java.io.*;
public class CopytxtTest {
private static final int BUFFER_SIZE = 1024;
public static void main(String[] args){
//创建字符读取流关联文件,读取数据
FileReader fr=null;
FileWriter fw=null;
try{
fr=new FileReader("demo.txt");
fw=new FileWriter("copy.txt");
//创建容器来临时存储数据
char[] buf=new char[BUFFER_SIZE];
//定义一个变量来记录读取的字符数,其实就是往数组李存储的字符个数。
int len=0;
while((len=fr.read())!=-1){
fw.write(buf,0,3);}
}catch(IOException e){
throw new RuntimeException("读写失败");
}finally{
if(fw!=null)
try {
fw.close();
} catch (IOException e) {
throw new RuntimeException("关闭失败");
}
if(fr!=null)
try {
fr.close();
} catch (IOException e) {
throw new RuntimeException("关闭失败");
}
}
}
}
|