/**
* 读取一个歌词文件,将里面的指定内容进行
* 修改,从而实现修改歌词的目的。
*/
package modify_lyrics;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class modify_lyrics {
public static final String filepath = "/home/zouxu/lyrics.txt";
public static void main(String[] args) {
File file1 = new File(filepath);
Reader reader = null;
try {
reader = new FileReader(file1);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader breader = new BufferedReader(reader);
StringBuffer content = new StringBuffer();
String line = null;
try {
while((line = breader.readLine()) != null)
{
content.append(line);
content.append(System.getProperty("line.separator"));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
breader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
breader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String content2 = content.toString().replaceAll("(男)", "这是男说的").replaceAll("(女)", "这是女说的");
Writer writer = null;
try {
writer = new FileWriter(file1);
} catch (IOException e) {
e.printStackTrace();
}
BufferedWriter bwriter = new BufferedWriter(writer);
try {
bwriter.write(content2);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bwriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
文件的内容修改
最新推荐文章于 2024-09-04 14:40:56 发布