This Java tutorial shows how to read a text file and store the values in an array.
Suppose you have a file having the rows as shown below:
java,30,120|python,23,178|c++,15,164;
The below example will read these values and will store them in an array.
Example: Reading Text File and Store Values in an Array in Java
public List loadPeopleFromFile(String path) { List result = new ArrayList<>(); try { try (Scanner s = new Scanner(new File("datos.txt"))) { s.useDelimiter("[|,;]"); while (s.hasNext()) { result.add(new Person(s.next(), s.nextInt(), s.nextInt())); } } } catch (IOException ex) { ex.printStackTrace(); } return result; }
See also:
Leave a comment