java缓冲字符流,复制文件,数组读取、写入

时间:2021-03-15 22:41:50
package cwj.bbb;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;



class StreamTest
{
public static void main(String[] args) throws IOException
{
/*
* 把路径下的文件/home/cwjy1202/hadoop/javaTest/input02.txt
* 复制到/home/cwjy1202/hadoop/javaTest/input016.txt
* 以缓冲字符流、按数组的方式读取、写入
* */
File file = new File("/home/cwjy1202/hadoop/javaTest/input02.txt");
Reader br = new BufferedReader(new FileReader(file));
Writer bw = new BufferedWriter(new FileWriter("/home/cwjy1202/hadoop/javaTest/input016.txt"));

char[] cbuf = new char[1024];
//数组读取
int len = br.read(cbuf);
while (-1 != len)
{
//数组写入
bw.write(cbuf, 0, len);
//数组读取
len = br.read(cbuf);
}

//刷新缓冲区的数据,强制写入目标文件
bw.flush();
bw.close();
br.close();
}
}