简体   繁体   中英

How to find lines in a text file containing specific strings and replace each whole line with a new string,?

Just starting out with Java on Android. Finding lots of example code to search for strings and replace them or remove the line where they occur.

But nothing to replace the whole line the strings occur on with a new string.

If there is an example I would be grateful for a pointer to it or some example code I can modify.

The file is never more than 50 lines.

There will always be two strings searched for and the line each occurs on will always be replaced with a completely new string.

Each string will only occur once in the file (though there are possibly some similar).

The line number is unknown (as is the text for the rest of the line).

The new line must be inserted into the text file at exactly the same line number as the original line.

Example where the text file is named: xyz-colors.txt

Search string one: yellow-XXc

New string to replace the complete line where string one occurred: John - black-XXe - new pin 13762

Search string two: green-BBa

New string to replace the complete line where string two occurred: Mike - red-XXd - new pin 14862

Thank you for any pointers.

Edit:

I have just about given up on this. So many examples but all find multiple occurrences of the string and this will be wasting resources and time, as I know for definite that there is only one occurrence. I will just ask them to restructure their files so I now the line number I need to replace and then go from there.

Edit2: The files cannot be restructured so I have to find an alternative to Paths.get

Edit3: I have just about resolved this. Using Scanner to get the line number and BufferedReader and FileReader/readline to get the full string for that line number. Now I just have to replace the string from the full line with another string containing the changes using FileWriter. I will post the full code when I am finished.

Let us approach it like this:

  • You have a file to read --> Figure out how to load it first

  • You have to update a file --> Figure out how to update a file.

  • Read all lines (Since it is max 50, shouldnt care about optimizations here)

  • Loop over each line, check if it contains the string you are searching for, replace Line with new Line.

  • Loop finished? Update your file, with updated list of Lines.

Look into using File.readAllLines() from Java >= 8

Some helpfull document for you: https://javarevisited.blogspot.com/2015/07/3-ways-to-read-file-line-by-line-in.html#axzz6dg6HEon1

Just a simple example as a starting point without exception handling:

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException {
        //path to your file
        String filename ="C:\\Temp\\xyz-colors.txt";
        //read content of file to list
        List<String> list = Files.readAllLines(Paths.get(filename));

        String searchOne = "yellow-XXc";
        String replacementOne = "John - black-XXe - new pin 13762";

        String searchTwo = "green-BBa";
        String replacementTwo = "Mike - red-XXd - new pin 14862";

        list.replaceAll(s -> s.contains(searchOne) ? replacementOne : s);
        list.replaceAll(s -> s.contains(searchTwo) ? replacementTwo : s);

        //write modified list to file
        Files.write(Paths.get(filename), list, Charset.forName("UTF-8"));
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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