简体   繁体   English

使用扫描仪类(java)更改文本文件中的特定文本

[英]change specific text in text file with scanner class (java)

I write this code that can search for the some specific text (such as word) in the text file with scanner class, but i want also to replace (old text to the new text) in the same old text locuation. 我编写了这段代码,可以使用扫描仪类在文本文件中搜索某些特定的文本(例如单词),但是我也想在相同的旧文本位置中替换(将旧文本替换为新文本)。 i find in the internet that i must used replaceAll method like ( replaceAll(old, new); ) but it does't work with the scanner class. 我在互联网上发现我必须使用replaceAll方法,例如(replaceAll(old,new);),但是它不适用于扫描仪类。

This is my code, it just search (if it existed ) write new text in new line without change the old one. 这是我的代码,它只是搜索(如果存在)在新行中写新文本,而不更改旧文本。

Do i need to change the method (to get the data) form scanner to FileReader ?? 我需要将表单扫描仪的方法(获取数据)更改为FileReader吗?

File file = new File("C:\\Users....file.txt");
Scanner input = new Scanner(System.in);
System.out.println("Enter the content you want to change:");
String Uinput = input.nextLine();
System.out.println("You want to change it to:");
String Uinput2 = input.nextLine();

Scanner scanner = new Scanner(file).useDelimiter(",");
BufferedWriter writer  = new BufferedWriter(new FileWriter(file, true));
while (scanner.hasNextLine()) {
    String lineFromFile = scanner.next();
    if (lineFromFile.contains(Uinput)) {
        lineFromFile = Uinput2;
        writer.write(lineFromFile); 
        writer.close();
        System.out.println("changed " + Uinput + " tO " + Uinput2);
        break;
    }
    else if (!lineFromFile.contains(Uinput)){
        System.out.println("Don't found " + Uinput);
        break;
    }
}

You cannot read from a file, then write to that same file. 您无法读取文件,然后再写入该文件。 You need 2 different files. 您需要2个不同的文件。

  while (read line from input file) { 
      if (NOT matches your search pattern)
            write line to output file.
      else  { // matches
          write start of line to your search pattern.
          write your replace string
          write from end of search pattern to end of line.
      }
   }

Unless your replace string is the same size as your search string, yes, you'll have to use 2 files. 除非替换字符串的大小与搜索字符串的大小相同,否则,您将必须使用2个文件。 Consider the file: 考虑文件:

 Blah
 Blah
 Blah

Now replace the letter 'a' with "The quick Brown Fox". 现在,将字母“ a”替换为“ the quick Brown Fox”。 If you replace the first line, you've overwritten the rest of the file. 如果替换第一行,则将覆盖文件的其余部分。 Now you can't read the 2nd line, so YES, you'll have to use 2 files. 现在您无法阅读第二行,因此是的,您将必须使用2个文件。

Here's another answer based on @Sedrick comment and your code. 这是基于@Sedrick注释和您的代码的另一个答案。 I'm adding it to your pseudo code. 我将其添加到您的伪代码中。

File file = new File("C:\\Users....file.txt");
Scanner input = new Scanner(System.in);
System.out.println("Enter the content you want to change:");
String Uinput = input.nextLine();
System.out.println("You want to change it to:");
String Uinput2 = input.nextLine();

Scanner scanner = new Scanner(file).useDelimiter(",");
java.util.List<String> tempStorage = new ArrayList<>();

while (scanner.hasNextLine()) {
    String lineFromFile = scanner.next();
    tempStorage.add(lineFromFile);
}

 // close input file here
// Open your write file here (same file = overwrite).

// now loop through temp storage searching for input string.
for (String currentLine : tempStorage ) {
     if (!lcurrentLine.contains(Uinput)){
        String temp = currentLine.replace(Uinput, Uinput2);
         write a line using temp variable  
     } else { // not replaced
         write a line using currentLine;  
 }
 // close write file here

By the way, you'll have to encase the reads writes with try catch to trap for IOExceptions. 顺便说一句,您必须用try catch包裹读取写入,以捕获IOExceptions。 That's how I knew it was pseudo code. 这就是我所知道的是伪代码。 There are plenty of examples for reading/writing a file on this web site. 在此网站上有许多读取/写入文件的示例。 It's easy to search for. 很容易搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM