BufferedReader reader = null;
BufferedWriter writer = null;
try {
// 有08.import java.util.List;的代码txt 该txt最好是UTF-8格式的,否则中文会乱码
reader = new BufferedReader(new FileReader("d:\\11.txt"));
// 去除08.的代码文件 文件格式是utf-8的
writer = new BufferedWriter(new FileWriter("d:\\11NEW.txt"));
String line = null;
do{
line = reader.readLine();
if (line != null){
System.out.println(line.substring(3));
writer.write(line.substring(3));
writer.newLine();
}
}while(line != null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (null != reader){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != writer){
try {
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedWriter writer = null;
try {
// 有08.import java.util.List;的代码txt 该txt最好是UTF-8格式的,否则中文会乱码
reader = new BufferedReader(new FileReader("d:\\11.txt"));
// 去除08.的代码文件 文件格式是utf-8的
writer = new BufferedWriter(new FileWriter("d:\\11NEW.txt"));
String line = null;
do{
line = reader.readLine();
if (line != null){
System.out.println(line.substring(3));
writer.write(line.substring(3));
writer.newLine();
}
}while(line != null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (null != reader){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != writer){
try {
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
本文提供了一个使用Java进行文件读写的示例程序,通过BufferedReader和BufferedWriter类从源文件中读取数据并去除特定字符后写入新文件。示例展示了如何处理文件不存在及输入输出异常。
1480

被折叠的 条评论
为什么被折叠?



