i am trying to read a .txt file in java and create a list of lists as to put every line of that .txt to another list. For every file i tried to do this all were fine but with the facebook_combined.txt.gz file which is at this link it doesnt do it the right way. Example:
if the first line of another .txt file is like this
52 99 45 61 70 45
and the second like this
70 80 65 91
then my code should create the list of lists named lines and lines must be like this:
line=[[52,99,45,61,70,45][70,80,65,91]].
But for the facebook_combinded.txt file if we suppose that its first line is like this 0 10 20 30 40 50
the same code creates the list of lists lines like this:
lines=[[0,1][0,2][0,3][0,4][0,5][0,...]].
The code i use is below:
ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();
//read the file
FileInputStream fstream = new FileInputStream("C:\\Users\\facebook_combined.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while (true)//while the file was read
{
String line = br.readLine();//split the file into the lines
if (line == null)
{
break;//if there are no more lines left
}
Scanner tokenize = new Scanner(line);// split the lines into tokens and make into an arraylist
ArrayList<String> tokens = new ArrayList<String>();
while (tokenize.hasNext()) //while there are still more
{
tokens.add(tokenize.next());
}
lines.add(tokens);
}
br.close();