import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyOboutFile {
/**
* direction:If you need to copy a file from one to another,the following
* code can do it. There has some steps:
* (1)Creates File objects including both file you need to use.
* (2)Creates File according to the giving pathname.
* (3)Reading and writing and don't forget close the input and output stream.
*
*/
public static void main(String[] args) throws IOException {
String[] path = { "e:/a.in", "e:/a.out" };
File in = new File(path[0]);
File out = new File(path[1]);
if (!in.exists()) {
System.out.println("系统找不到需要拷贝的文件");
return;
}
if (!out.exists()) {
if (!out.getParentFile().exists()) {
out.getParentFile().mkdirs();
out.createNewFile();
} else {
out.createNewFile();
}
}
BufferedReader br = new BufferedReader(new FileReader(in));
BufferedWriter bf = new BufferedWriter(new FileWriter(out));
int a;
while ((a = br.read()) != -1) {
bf.write(a);
}
//You can also use this way to copy,but this way may lead to add a newline character.
// String s=null;
// while((s = br.readLine()) != null){
// bf.write(s);
// bf.newLine();
// }
br.close();
bf.close();
System.out.println("数据已写入");
}
}
Copying about text file
最新推荐文章于 2024-06-01 12:01:48 发布