Created
November 23, 2015 14:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 | |
20 | |
30 | |
100 200 | |
1001 | |
221 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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