采用文件字符输入流和文件字符输出流把 Dwordatxt 复制到Ewordatxt分别采用一次读取一个字符的方式和一次读取一个字符数组的方式
一次读取一个字符的方式:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileCopyDemo {
public static void main(String[] args) {
FileReader reader = null;
FileWriter writer = null;
try {
reader = new FileReader("D:\\word\\a.txt");
writer = new FileWriter("E:\\word\\a.txt");
int c;
while ((c = reader.read()) != -1) {
writer.write(c);
}
System.out.println("文件复制成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
一次读取一个字符数组的方式:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileCopyDemo {
public static void main(String[] args) {
FileReader reader = null;
FileWriter writer = null;
try {
reader = new FileReader("D:\\word\\a.txt");
writer = new FileWriter("E:\\word\\a.txt");
char[] buffer = new char[1024];
int len;
while ((len = reader.read(buffer)) != -1) {
writer.write(buffer, 0, len);
}
System.out.println("文件复制成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
``
原文地址: http://www.cveoy.top/t/topic/fFd8 著作权归作者所有。请勿转载和采集!