Java替换文本文件中的行

Eveno

如何替换在文本文件中找到的一行文本?

我有一个字符串,例如:

Do the dishes0

我想用以下方式更新它:

Do the dishes1

(反之亦然)

我该怎么做?

ActionListener al = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JCheckBox checkbox = (JCheckBox) e.getSource();
                    if (checkbox.isSelected()) {
                        System.out.println("Selected");
                        String s = checkbox.getText();
                        replaceSelected(s, "1");
                    } else {
                        System.out.println("Deselected");
                        String s = checkbox.getText();
                        replaceSelected(s, "0");
                    }
                }
            };

public static void replaceSelected(String replaceWith, String type) {

}

顺便说一句,我只想替换已读取的行。不是整个文件。

迈克尔·雅沃斯基

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

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");   
}

原始文本文件内容:

洗碗0
喂狗0
打扫房间1

输出:

洗碗0
喂狗0
打扫我的房间1
----------------------------------
洗碗1
喂狗0
清洗我的房间1

新的文本文件内容:

洗碗1
喂狗0
打扫房间1


并请注意,如果文本文件是:

洗碗1
喂狗0
打扫房间1

并且您使用了方法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 lines
public 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.");
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Java替换文本文件中的行

来自分类Dev

Linux替换文本文件中的行

来自分类Dev

替换文本文件中的行(最后)bash

来自分类Dev

如何替换文本文件中的行?

来自分类Dev

用其他文本文件中的行替换文本文件中的行

来自分类Dev

使用VBS替换文本文件中的文本

来自分类Dev

从另一个文本文件替换文本文件中的行

来自分类Dev

匹配并替换文本文件的特定行

来自分类Dev

使用 sed 替换文本文件中 2 个特定行之间的文本

来自分类Dev

Java程序动态替换文本文件

来自分类Dev

使用php替换文本文件中的特定行,同时保留其余文件

来自分类Dev

用新行替换文本文件中的行

来自分类Dev

用新行替换文本文件中的行

来自分类Dev

如何替换文本文件中的多行?

来自分类Dev

使用Powershell替换文本文件中的路径

来自分类Dev

替换文本文件批中的特殊字符

来自分类Dev

复制并替换文本文件中的模式

来自分类Dev

替换文本文件中的参数

来自分类Dev

用sed替换文本文件中的路径

来自分类Dev

如何替换文本文件中的字母?

来自分类Dev

在Flask中搜索并替换文本文件

来自分类Dev

用“_”替换文本文件中的元音的函数

来自分类Dev

替换文本文件中的多行

来自分类Dev

如何替换文本文件中 url 的值

来自分类Dev

Python如何替换文本文件中特定行中的特定单词?

来自分类Dev

替换文本文件Golang中的一行

来自分类Dev

删除行并替换文本文件中的字符串

来自分类Dev

仅替换文本文件中的一行

来自分类Dev

用CR + LF替换文本文件中的LF,NEL行结尾