Creating a txt file from another txt file

1

I have a file in this format (zero indicates 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 conditional to perform the exchange of numbers signals, that each number (with signal exchanged) has its own line and that each line should end with zero.

    
asked by anonymous 30.08.2016 / 01:02

1 answer

2

Read row by line of file and check with 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 verify that the line ends with a suffix, use the String#endsWith .

    
30.08.2016 / 01:45