how to read a file into an array java code example
Example 1: 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 2: 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();
Example 3: examples of java programs that read files and put them into arrays
Scanner scanner = new Scanner(new File("input.txt"));
int [] tall = new int [100];
int i = 0;
while(scanner.hasNextInt())
{
tall[i++] = scanner.nextInt();
}
Example 4: from file to array java
ArrayList<String> result = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
while (br.ready()) {
result.add(br.readLine());
}
}