替换织物中的一行,保留旧行的残留部分。

huangapple go评论82阅读模式
英文:

Replacing a line in a textile leaving remains of oldLine

问题

我有一个文本文件,名为```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```时,它仍然保留在输出中,如下所示。

**代码**
```java
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("输入要编辑的项目");
	replacee = scan.next();
	scan.nextLine();
	System.out.println("输入更新的信息");
	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

输入要编辑的项目
cats
输入更新的信息
turtles, 5, 5

文本文件输出

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

<details>
<summary>英文:</summary>

I have a textfile, ```inventory.txt```, with 

cats, 10, 15
dogs, 10, 15

I want to run a code where I can input ```cats``` as ```replacee``` and ```turtles, 5, 5``` as the ```replacer```, giving me, 

turtles, 5, 5
dogs, 10, 15


however, when I do this, everything after the first comma remains in addition to my ```replacer```. I can replace ```cats``` with JUST ```turtles``` with no ```5, 5```, giving me 

turtles, 10, 15
dogs, 10, 15

but when I try adding the ```5, 5``` after the first comma remains, output below.


**Code**

public void modifyItems() throws IOException {

	File file = new File(&quot;src/inventory.txt&quot;);
	String line = &quot;&quot;;
	String oldLine = &quot;&quot;;
	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(&quot;enter the item you want to edit&quot;);
	replacee = scan.next();
	scan.nextLine();
	System.out.println(&quot;enter the updated information&quot;);
	replacer = scan.nextLine();
	String newLine = oldLine.replaceAll(replacee, replacer);
	
	BufferedWriter writer = new BufferedWriter(new FileWriter(file));
	writer.write(newLine);
	writer.flush();
	writer.close();
}

**Output**

cats, 10, 15
dogs, 10, 15

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


**Textfile output**

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


</details>


# 答案1
**得分**: 0

```java
// 字符串的 `replaceAll` 方法执行的是你在代码中看到的操作。
// 它会将你输入的文本替换为你输入的库存字符串。

// 你需要使用的方法是字符串的 `startsWith` 方法。

// 你需要保留一个已读行的数组,这样你就可以找到要替换的正确行。

public void modifyItems() throws IOException {

    File file = new File("src/inventory.txt");

    // 将100替换为足够大的数字,以容纳整个文件。
    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("输入要编辑的项目");
    String replacee = scan.nextLine();
    System.out.println("输入更新后的信息");
    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();
}
英文:

The String replaceAll method does what you've seen in your code. It replaces the text you input with the inventory string you input.

The method you want to use is the String startsWith method.

You need to keep an array of lines read, so you can find the correct line to replace.

public void modifyItems() throws IOException {

	File file = new File(&quot;src/inventory.txt&quot;);

	// 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 = &quot;&quot;;
	while ((line = reader.readLine()) != null) {
		oldLine[count] = line + System.lineSeparator();
		System.out.println(line);
		count++;
	}
	reader.close();
	
	System.out.println(&quot;enter the item you want to edit&quot;);
	String replacee = scan.nextLine();
	System.out.println(&quot;enter the updated information&quot;);
	String replacer = scan.nextLine();

	BufferedWriter writer = new BufferedWriter(
			new FileWriter(file));
	for (int i = 0; i &lt; count; i++) {
		if (oldLine[i].startsWith(replacee)) {
			writer.write(replacer);
		} else {
			writer.write(oldLine[i]);
		}
	}
	writer.flush();
	writer.close();
}

huangapple
  • 本文由 发表于 2020年5月5日 11:06:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/61605055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定