Creating txt file from another txt file

Asked

Viewed 51 times

1

I have a file in this format (the zero indicates that the line has finished):

-1 2 0
-2 3 0
$
-1 3 0

What do I do to identify if the file line has a $? Because I have to implement this condition to effect the exchange of signals of the numbers, that each number (with signal exchanged) has its own line and that each line must end with zero.

1 answer

2


Read line by line from the file and check with the function String#contains if the line contains the desired character:

try (BufferedReader br = new BufferedReader(new FileReader("foo.txt"))) {
    for(String linha; (linha = br.readLine()) != null; ) {
        if(linha.trim().contains("$")) {
            // A linha contém "$", fazer algo aqui...
        } else {
            // A linha não contém "$"
        }
    }
} catch (IOException err) {
    System.err.println("O arquivo nao pode ser aberto!");
    System.err.println(err.getMessage());
}

To check if the line ends with a suffix, use the function String#endsWith.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.