【问题标题】:Replacing a line in a textile leaving remains of oldLine更换纺织品中的一条线,留下 oldLine 的残留物
【发布时间】:2020-08-19 15:16:28
【问题描述】:

我有一个文本文件,inventory.txt,带有

cats, 10, 15
dogs, 10, 15

我想运行一个代码,我可以输入cats 作为replacee 和turtles, 5, 5 作为replacer,给我,

turtles, 5, 5
dogs, 10, 15

但是,当我这样做时,除了我的replacer 之外,第一个逗号之后的所有内容都将保留。我可以将cats 替换为仅turtles 而没有5, 5,给我

turtles, 10, 15
dogs, 10, 15

但是当我尝试在第一个逗号保留后添加5, 5 时,输出如下。

代码

public void modifyItems() throws IOException {

        File file = new File("src/inventory.txt");
        String line = "";
        String oldLine = "";
        String replacee;
        String replacer;

        BufferedReader reader = new BufferedReader(new FileReader(file));

        while ((line = reader.readLine()) != null) {
            oldLine += line + System.lineSeparator();

        }
        System.out.println(oldLine);
        reader.close();

        System.out.println("enter the item you want to edit");
        replacee = scan.next();
        scan.nextLine();
        System.out.println("enter the updated information");
        replacer = scan.nextLine();
        String newLine = oldLine.replaceAll(replacee, replacer);

        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.write(newLine);
        writer.flush();
        writer.close();
    }

输出

cats, 10, 15
dogs, 10, 15

enter the item you want to edit
cats
enter the updated information
turtles, 5, 5

文本文件输出

turtles, 5, 5, 10, 15
dogs, 10, 15

【问题讨论】:

    标签: java text methods replace


    【解决方案1】:

    String replaceAll 方法执行您在代码中看到的操作。它将您输入的文本替换为您输入的库存字符串。

    您要使用的方法是StringstartsWith 方法。

    您需要保留读取的行数组,以便找到要替换的正确行。

    public void modifyItems() throws IOException {
    
        File file = new File("src/inventory.txt");
    
        // Replace 100 with a large enough number to hold
        // the whole file.
        String[] oldLine = new String[100];
    
        BufferedReader reader = new BufferedReader(
                new FileReader(file));
        int count = 0;
        String line = "";
        while ((line = reader.readLine()) != null) {
            oldLine[count] = line + System.lineSeparator();
            System.out.println(line);
            count++;
        }
        reader.close();
    
        System.out.println("enter the item you want to edit");
        String replacee = scan.nextLine();
        System.out.println("enter the updated information");
        String replacer = scan.nextLine();
    
        BufferedWriter writer = new BufferedWriter(
                new FileWriter(file));
        for (int i = 0; i < count; i++) {
            if (oldLine[i].startsWith(replacee)) {
                writer.write(replacer);
            } else {
                writer.write(oldLine[i]);
            }
        }
        writer.flush();
        writer.close();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 2014-02-15
      • 2015-01-31
      • 2020-10-19
      • 1970-01-01
      • 2011-10-26
      相关资源
      最近更新 更多