txt file to array java code example
Example 1: java import text file into arraylist
List<String> list = Files.readAllLines(new File("input.txt").toPath(), Charset.defaultCharset() );
Example 2: java text file to arraylist
Scanner s = new Scanner(new File("filepath"));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
list.add(s.next());
}
s.close();
Example 3: from file to array java
BufferedReader abc = new BufferedReader(new FileReader(myfile));
List<String> lines = new ArrayList<String>();
while((String line = abc.readLine()) != null) {
lines.add(line);
System.out.println(data);
}
abc.close();
String[] data = lines.toArray(new String[]{});
Example 4: from file to array java
BufferedReader bufferedReader = new BufferedReader(new FileReader(myfile));
String []data;
data = new String[10];
for (int i=0; i<lines.size(); i++) {
data[i] = abc.readLine();
System.out.println(data[i]);
}
abc.close();