【发布时间】:2015-02-16 04:24:15
【问题描述】:
我有一个文本文件,其中包含用“--”分隔的各种键值对。 以下是我到目前为止的代码
File file = new File("C:\\StateTestFile.txt");
BufferedWriter out = new BufferedWriter(file.getAbsolutePath());
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
if(line.contains("content_style")) {
//Write to the line currently reading and save back to the file
}
}
br.close();
out.close();
我想要做的是阅读这个文本文件并用我指定的东西替换特定行的值。所以我想找到'content_style'行并将'posh'替换为'dirty'。
我该怎么做?
【问题讨论】:
-
你能问一个更具体的问题吗?我们不是来为你做任务的。请参阅How do I ask a good question? 了解更多信息。
-
参见
String的replace和replaceAll方法。后者也可以使用正则表达式 -
您可以完全按照您的描述执行此操作:读取文件、更改字符串、写入文件。如果您需要更详细的答案,则需要付出一些努力,例如您到目前为止所尝试的内容。
-
我在这里看到的其他有用方法是
String.split和String.contains -
@Grice 我已经添加了我目前拥有的代码。希望它更清楚。