How to save the Strings of a file read? [duplicate]

1

Personally I'm reading from a .txt file and would like to save all the strings how could I save it all? Below is my writing code that I'm using, in case I'd like to save the String line.

public static void main(String[] args) throws IOException {
              try {
                  File f = new File ("c:/teste.txt");
                  InputStream is = new FileInputStream("c:/teste.txt");
                    InputStreamReader isr = new InputStreamReader(is);
                    BufferedReader reader = new BufferedReader(isr);
                    String linha = reader.readLine();


                    while (linha != null) {



                        System.out.println(linha);

                        linha = reader.readLine();

                        }
                        reader.close();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
asked by anonymous 22.11.2017 / 02:39

1 answer

3

Do this:

List<String> linhas = Files.readAllLines("c:/teste.txt")

Much easier, right?

Documentation .

    
22.11.2017 / 02:46