方法一:InputStreamReader+OutputStreamWriter
需要绑定输入输出流,可以设定字符集
public class CopyFileTest2 {
public static void main(String[] args) throws IOException {
File file1 = new File("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本.txt");
File file2 = new File("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本3.txt");
FileInputStream fis = null;
FileOutputStream fos = null;
InputStreamReader reader = null;
OutputStreamWriter writer = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
reader = new InputStreamReader(fis, "utf-8");
writer = new OutputStreamWriter(fos, "utf-8");
char[] chars = new char[64];
int len = -1;
do {
len = reader.read(chars);
if (len!=-1){
writer.write(chars,0,len);
}
} while (len != -1);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
if (reader != null) {
reader.close();
}
if (fos != null) {
fos.close();
}
if (fis != null) {
fis.close();
}
}
}
}
方法二:FileReader+FileWriter
不需要手动绑定输入输出流,不能设定字符集
public class CopyFileTest3 {
public static void main(String[] args) throws IOException {
FileReader reader = null;
FileWriter writer = null;
try {
reader = new FileReader("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本.txt");
writer = new FileWriter("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本4.txt");
char[] chars = new char[64];
int len = -1;
do {
len = reader.read(chars);
if (len != -1) {
writer.write(chars, 0, len);
}
} while (len != -1);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
}
}
}
方法三:BufferedReader+BufferedWriter
缓冲字符流,有一个默认大小为8192的字符数组 char[] 作为缓冲区,在构造方法中可以指定缓冲区大小,需要绑定字符输入输出流,BufferedWriter 的 flush() 方法将缓冲区数据立即写入文件。
public class CopyFileTest4 {
public static void main(String[] args) throws IOException {
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
fr = new FileReader("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本.txt");
fw = new FileWriter("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本5.txt");
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
char[] chars = new char[64];
int len = -1;
do {
len = br.read(chars);
if (len != -1) {
bw.write(chars,0,len);
bw.flush();
}
}while (len!=-1);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
br.close();
}
if (bw != null) {
bw.close();
}
if (fr != null) {
fr.close();
}
if (fw != null) {
fw.close();
}
}
}
}
方法四:BufferedReader+BufferedWriter 按行读取与写入
BufferedReader 的 readLine() 读取一行 BufferedWriter 的 write() 写入一行 BufferedWriter 的 newLine() 写入一个换行符 BufferedWriter 的 flush() 将缓冲区字符立即写入文件
public class CopyFileTest5 {
public static void main(String[] args) throws IOException {
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
fr = new FileReader("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本.txt");
fw = new FileWriter("C:\\Users\\Administrator\\Desktop\\FileTest\\测试文本6.txt");
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
br.close();
}
if (bw != null) {
bw.close();
}
if (fr != null) {
fr.close();
}
if (fw != null) {
fw.close();
}
}
}
}
|