Read file, replace string and create a new one with all content

MrPencil

I am trying to replace ? with - in my text document but just the ArrayList<String> is being written in the new file without all lines of the old one. How can I fix that?

File file = new File("D:\\hl_sv\\L09MF.txt");

ArrayList<String> lns = new ArrayList<String>();
Scanner scanner;
try {

    scanner = new Scanner(file);

    int lineNum = 0;
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        lineNum++;
        if (line.contains("?")) {
            line = line.replace("?", "-");
            lns.add(line);

            // System.out.println("I found it on line " + lineNum);
        }
    }
    lines.clear();
    lines = lns;
    System.out.println("Test: " + lines);

    FileWriter writer;
    try {
        writer = new FileWriter("D:\\hl_sv\\L09MF2.txt");
        for (String str : lines) {
            writer.write(str);
        }

        writer.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
Elliott Frisch

I don't understand why you're storing the lines in a List to begin with. I would perform the transform and print while I read. You don't need to test for the presence of the ? (replace won't alter anything if it isn't present). And, I would also use a try-with-resources. Something like

File file = new File("D:\\hl_sv\\L09MF.txt");
try (PrintWriter writer = new PrintWriter("D:\\hl_sv\\L09MF2.txt");
        Scanner scanner = new Scanner(file)) {
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        writer.println(line.replace('?', '-'));
    }
} catch (Exception e) {
    e.printStackTrace();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Shell Read string from one file and replace in other file

From Dev

Replace a string by the content of a file

From Dev

Replace one string with the content pulled from other file

From Dev

Replace Text in Word file with Excel file content in one cell with line breaks. Replace All

From Dev

Replace content of one string with another?

From Dev

replace file with new one with timestamp

From Dev

Replace string with multiline file content

From Dev

Replace string with multiline file content

From Dev

Replace all UpperLetters in a string with a new String

From Dev

CMD Batch File to Read Files in Directory and Create new files with Content from the Name

From Dev

Replace QML file content by another one

From Dev

replace string with string+new line in file

From Dev

Read file, search text and replace all the line

From Dev

Does String.Replace() create a new string if there's nothing to replace?

From Dev

Is it possible to create dynamic content on file read operation?

From Dev

how to replace 2 new lines by one in the file

From Dev

how to replace 2 new lines by one in the file

From Dev

Replace string content between quotes in file

From Dev

Replace a multiline string with the content of a file.txt

From Dev

Create a new text file with the name and the content of others

From Dev

Create a new text file with the name and the content of others

From Dev

Regular Expression to Replace All But One Character In String

From Dev

Replace all occurences of one word in string in python

From Dev

Read a file and replace ${1}, ${2}... value with string

From Dev

Python read a file replace a string in a word

From Dev

Replace one string and create a column with another

From Dev

Read text file, create new rows by separator

From Dev

Merge content of one file into another file by replace a function

From Dev

Java: replace only one line/string in the file

Related Related

  1. 1

    Shell Read string from one file and replace in other file

  2. 2

    Replace a string by the content of a file

  3. 3

    Replace one string with the content pulled from other file

  4. 4

    Replace Text in Word file with Excel file content in one cell with line breaks. Replace All

  5. 5

    Replace content of one string with another?

  6. 6

    replace file with new one with timestamp

  7. 7

    Replace string with multiline file content

  8. 8

    Replace string with multiline file content

  9. 9

    Replace all UpperLetters in a string with a new String

  10. 10

    CMD Batch File to Read Files in Directory and Create new files with Content from the Name

  11. 11

    Replace QML file content by another one

  12. 12

    replace string with string+new line in file

  13. 13

    Read file, search text and replace all the line

  14. 14

    Does String.Replace() create a new string if there's nothing to replace?

  15. 15

    Is it possible to create dynamic content on file read operation?

  16. 16

    how to replace 2 new lines by one in the file

  17. 17

    how to replace 2 new lines by one in the file

  18. 18

    Replace string content between quotes in file

  19. 19

    Replace a multiline string with the content of a file.txt

  20. 20

    Create a new text file with the name and the content of others

  21. 21

    Create a new text file with the name and the content of others

  22. 22

    Regular Expression to Replace All But One Character In String

  23. 23

    Replace all occurences of one word in string in python

  24. 24

    Read a file and replace ${1}, ${2}... value with string

  25. 25

    Python read a file replace a string in a word

  26. 26

    Replace one string and create a column with another

  27. 27

    Read text file, create new rows by separator

  28. 28

    Merge content of one file into another file by replace a function

  29. 29

    Java: replace only one line/string in the file

HotTag

Archive