D
D
Dmitry Krapivin2018-07-08 21:37:25
Java
Dmitry Krapivin, 2018-07-08 21:37:25

How to save parsed from txt file to ArrayList?

Hello
I wrote a code that parses text from a txt file. The data is output.
I want to extract certain data from the parsing result. But for this, as I understand it, you need to save the results of parsing, and not just output.
Please tell me how to save the result of parsing into an array (arraylist), tell me please?
Content of ddd.txt file:
TENCM 135 091118
Here is my code:

public class Parser {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("ddd.txt");
        Scanner scan = new Scanner(fr);


        while (scan.hasNextLine()) {
            System.out.println(scan.nextLine());
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MaxLich, 2018-07-09
@MaxLich

final String myPath = "C:\myFile.txt";
try {
      List<String> lines = Files.readAllLines(Paths.get(myPath ));
} catch (IOException e) {
      e.printStackTrace();
}

A
Andrey Valitov, 2018-07-08
@Andrvat

As an option, you can separate the input data by a space character, and then move it from the array to the sheet in a loop.

String[] split = scan.nextLine().split(" ");
for (String temp : split) 
    text.add(temp);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question