推荐使用Buffered进行读写字符(速度快):
(1)带有缓存
(2)可读一行
1、字符流读写–带有缓存的批量字符读写Buffered(可读一行)
// 带有缓存的批量字符读写
// 且可以读一行
public void test3() {
File fromFile = new File("D:/123.txt");
File toFile = new File("E:/123.txt");
BufferedReader reader = null;
BufferedWriter writer = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile), "utf-8"));
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toFile), "utf-8"));
// 带缓冲区的reader可以,每次读一行。如果已到达流末尾,则返回null
String line = null;
while((line=reader.readLine())!=null) {
System.out.println(line);
// 每次写出一行,但是'\n'换行的标志需要在此处显示的加上
writer.write(line+"\n");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(writer!=null) {
writer.close();
}
if(reader!=null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、将给定的文件路径读出字符串 并返回
public static String readStringAndString(String filePath) {
StringBuffer buffer = new StringBuffer();
File file = new File(filePath);
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
String line=null;
while((line=reader.readLine())!=null) {
buffer.append(line).append("\n");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(reader!=null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return buffer.toString();
}
3、字符流读写–单个字符读写
// 单个字符读写
public void test1() {
File fromFile = new File("D:/123.txt");
File toFile = new File("E:/123.txt");
Reader reader = null;
Writer writer = null;
try {
// reader = new FileReader(fromFile); // 直接读写出现乱码,需要进行包装
// writer = new FileWriter(toFile);
reader = new InputStreamReader(new FileInputStream(fromFile), "utf-8");
writer = new OutputStreamWriter(new FileOutputStream(toFile), "utf-8");
int ch = -1;
while ((ch = reader.read()) != -1) {
System.out.println((char) ch);
writer.write(ch);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer!=null) {
writer.close();
}
if(reader!=null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4、字符流读写–批量字符读写
public void test2() {
File fromFile = new File("D:/123.txt");
File toFile = new File("E:/123.txt");
Reader reader = null;
Writer writer = null;
try {
reader = new InputStreamReader(new FileInputStream(fromFile), "utf-8");
writer = new OutputStreamWriter(new FileOutputStream(toFile), "utf-8");
// 定义一个char的数组,用于存放批量读取的字符数据
char[] chars = new char[1024];
int length = -1;
// 每次将字符读取到char的数组,并返回读取的个数,当到达流的末尾则返回-1
while ((length = reader.read(chars)) != -1) {
System.out.println(Arrays.toString(chars));
System.out.println("读取的字符数量:" + length);
writer.write(chars, 0, length);
}
} catch (UnsupportedEncodingException | FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(writer!=null) {
writer.close();
}
if(reader!=null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}