Skip to content

Instantly share code, notes, and snippets.

@sudheesh001
Created November 23, 2015 14:45
Show Gist options
  • Save sudheesh001/91a1ee9d6431e5ecb8c5 to your computer and use it in GitHub Desktop.
Save sudheesh001/91a1ee9d6431e5ecb8c5 to your computer and use it in GitHub Desktop.
Java file read to array, use collections to sort if needed. Just a sample
10
20
30
100 200
1001
221
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class filetoarray {
public static void main(String[] args) {
Scanner file = null;
ArrayList<Integer> list = new ArrayList<Integer>();
try {
file = new Scanner(new File("file1.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while(file.hasNext()){
if (file.hasNextInt())
list.add(file.nextInt());
else file.next();
}
for (Integer i: list)
System.out.println(i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment