栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java替换文本文件中的行

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java替换文本文件中的行

在底部,我有一个通用的解决方案来替换文件中的行。但是首先,这是眼前特定问题的答案。辅助功能:

public static void replaceSelected(String replaceWith, String type) {    try {        // input the file content to the StringBuffer "input"        BufferedReader file = new BufferedReader(new FileReader("notes.txt"));        StringBuffer inputBuffer = new StringBuffer();        String line;        while ((line = file.readLine()) != null) { inputBuffer.append(line); inputBuffer.append('n');        }        file.close();        String inputStr = inputBuffer.toString();        System.out.println(inputStr); // display the original file for debugging        // logic to replace lines in the string (could use regex here to be generic)        if (type.equals("0")) { inputStr = inputStr.replace(replaceWith + "1", replaceWith + "0");         } else if (type.equals("1")) { inputStr = inputStr.replace(replaceWith + "0", replaceWith + "1");        }        // display the new file for debugging        System.out.println("----------------------------------n" + inputStr);        // write the new string with the replaced line OVER the same file        FileOutputStream fileOut = new FileOutputStream("notes.txt");        fileOut.write(inputStr.getBytes());        fileOut.close();    } catch (Exception e) {        System.out.println("Problem reading file.");    }}

然后调用它:

public static void main(String[] args) {    replaceSelected("Do the dishes", "1");   }

原始文本文件内容:

Do the dishes0Feed the dog0Cleaned my room1

Output:

Do the dishes0Feed the dog0Cleaned my room1


Do the dishes1Feed the dog0Cleaned my room1

New text file content:

Do the dishes1Feed the dog0Cleaned my room1

And as a note, if the text file was:

Do the dishes1Feed the dog0

Cleaned my room1并且你使用了方法replaceSelected(“Do the dishes”, “1”);,它将不会更改文件。

由于这个问题非常具体,因此我将在此处为将来的读者添加一个更通用的解决方案(基于标题)。

// read file one line at a time// replace line as you read the file and store updated lines in StringBuffer// overwrite the file with the new linespublic static void replaceLines() {    try {        // input the (modified) file content to the StringBuffer "input"        BufferedReader file = new BufferedReader(new FileReader("notes.txt"));        StringBuffer inputBuffer = new StringBuffer();        String line;        while ((line = file.readLine()) != null) { line = ... // replace the line here inputBuffer.append(line); inputBuffer.append('n');        }        file.close();        // write the new string with the replaced line OVER the same file        FileOutputStream fileOut = new FileOutputStream("notes.txt");        fileOut.write(inputBuffer.toString().getBytes());        fileOut.close();    } catch (Exception e) {        System.out.println("Problem reading file.");    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/451862.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号