FileReader known class is used to read the data stream is an input, and the class is used to write data FileWriter output stream.
Then the combination of the two, we can put a file using FileWriter read out, and then use this FileWriter read out into a file, copy a file to complete the operation.
Because there must be a file is copied, I have to say in their own java project built in a file named sourceFile.txt of (the text document and java project created in the same document) which reads as follows
next It is code implementation
package zjh;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyFile {
public static void main(String[] args) throws IOException { //必须丢出IOException
FileReader fr = new FileReader("sourceFile.txt"); //创建读数据的对象,读我创建好的文档
FileWriter fw = new FileWriter("copyFile.txt"); //创建写数据的对象,写进名为copyFile.txt的文档
int ch; //调用读文档的一个方法 int read()并且用while循环读出,直到读完整个文档
while((ch=fr.read())!=-1) {
fw.write(ch); //把读出的东西一个一个写进所要求写进的文档中
}
//释放资源
fr.close();
fw.close();
}
}
Here is a good document copying
There members of the method of read data int read (char [] cbuf) to complete the copying operation in this way the code below
which is a data reading method of a character array, and int read () can only read a character
package zjh;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyFile2 {
public static void main(String[] args) throws IOException {
//创建输入流对象
FileReader fr = new FileReader("sourceFile.txt");
//创建输出流对象
FileWriter fw = new FileWriter("copyFile.txt");
//读写数据
char[] chs = new char[1024];
int len;
while((len=fr.read(chs))!=-1) {
fw.write(chs, 0, len);
}
//释放资源
fw.close();
fr.close();
}
}