10 Replies - 1385 Views - Last Post: 19 April 2010 - 09:57 PM Rate Topic: -----

#1 kr4vk0dr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 25-October 09

ArrayList to File //only copies part

Posted 17 April 2010 - 12:01 PM

Hello, I am working on an assignment which will be able to read and sort a large file, by copying and merging it parts at a time. So to start I am attempting to copy the first 4096 lines of the input file into an ArrayList, then copy that sorted ArrayList into a new temp file. But when I try and run a loop like this to read the amount of lines i want....

public static void Feed_file ()

	{  	
		int Line_num = 1;

		while(Feed.hasNextLine()){
		if (Line_num == 4096) break;		
		String Line = Feed.nextLine();

		L.add(Line_num + " " + Line);

		Line_num++;

		}System.out.println(L);
//shows me the arraylist has filled properly 
		//Collections.sort(L);

		for (String s: L)
		Put.println(s);	 	
		
                L.clear();
		}
	}


The arraylist will be filled as i was expecting but the new file I am attempting to copy the arrays contents to stops abruptly at line 3787 and doesnt even manage to write the line number, only "3".

even if i try
  L.add(Line);


instead of
L.add(Line_num + " " + Line);


the file will stop in the middle of line 3817

Why is this?

This post has been edited by kr4vk0dr: 17 April 2010 - 12:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: ArrayList to File //only copies part

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: ArrayList to File //only copies part

Posted 17 April 2010 - 01:14 PM

How is "L" declared?
Was This Post Helpful? 0
  • +
  • -

#3 kr4vk0dr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 25-October 09

Re: ArrayList to File //only copies part

Posted 17 April 2010 - 02:42 PM

View Postn8wxs, on 17 April 2010 - 12:14 PM, said:

How is "L" declared?


public static ArrayList <String> L = new ArrayList <String> ();


Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: ArrayList to File //only copies part

Posted 17 April 2010 - 02:54 PM

I figured. :)

Since you know how many lines you're gonna read from the file, try this:

public static ArrayList <String> L = new ArrayList <String> (5120);



5120 is bigger than 4096 so the list won't grow if you stay below that size. :)

You can declare an initial size for the number of elements the list will hold:

ArrayList(int initialCapacity) Constructs an empty list with the specified initial capacity.
Was This Post Helpful? 0
  • +
  • -

#5 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 515
  • Joined: 14-January 09

Re: ArrayList to File //only copies part

Posted 17 April 2010 - 03:12 PM

You know, the VM might just be running out of memory?
Was This Post Helpful? 0
  • +
  • -

#6 kr4vk0dr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 25-October 09

Re: ArrayList to File //only copies part

Posted 17 April 2010 - 03:41 PM

View Postn8wxs, on 17 April 2010 - 01:54 PM, said:

I figured. :)

Since you know how many lines you're gonna read from the file, try this:

public static ArrayList <String> L = new ArrayList <String> (5120);



5120 is bigger than 4096 so the list won't grow if you stay below that size. :)

You can declare an initial size for the number of elements the list will hold:

ArrayList(int initialCapacity) Constructs an empty list with the specified initial capacity.



View PostGhlavac, on 17 April 2010 - 02:12 PM, said:

You know, the VM might just be running out of memory?


Both of these ideas crossed my mind but whats wierd is that the strings are simply not being written for example when i uncomment the print statement to print the arraylist the last line of the list is what i want it to be.

, 4095 would capture it on his own account. The next day he attempted

^^perfect yay everybodys happy.

but when i scroll down to the last line of my created file i have

3872 The Stag at the

it doesnt even finish the sentence, so wierd. I have a feeling there i am doing something very stupid or there is a very simple solution.
The file im reading from has numerous lines of white space (which i need to count as a line) would this make any difference.

And thanks for all the help so far
Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: ArrayList to File //only copies part

Posted 17 April 2010 - 04:46 PM

How is "Put" defined?
Was This Post Helpful? 0
  • +
  • -

#8 kr4vk0dr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 25-October 09

Re: ArrayList to File //only copies part

Posted 17 April 2010 - 05:52 PM

View Postn8wxs, on 17 April 2010 - 03:46 PM, said:

How is "Put" defined?


heres the code ive got so far....

import java.util.*;
import java.io.*;

public class Temp {

	public static ArrayList <String> L = new ArrayList <String> ();
	public static Scanner Feed = null;
	public static PrintWriter Put = null;
	
public static void main (String[] args)throws FileNotFoundException 

	{
	Feed = new Scanner (new File(args[0]));
	int fir = 0, sec = 0;
	String temp = MakeFile(fir, sec);	
	File f = new File(temp);	
	Put = new PrintWriter (temp);	
	Feed_file();	

}	

	public static void Feed_file ()

	{  	
		int Line_num = 1;
		String Line;
		
                while(Feed.hasNextLine()){
		if (Line_num == 4096) break;		
		Line = Feed.nextLine();
		L.add(Line_num + " " + Line);
		Line_num++;
		} 

		Feed.close();
		//System.out.println(L);
		//Collections.sort(L);

		for (String s: L)
		Put.println(s);	 	
		
		L.clear();

		}

public static String MakeFile(int fir, int sec)
	{
	StringBuilder name = new StringBuilder("temp___.txt");
	name.insert(5, fir);
	
	if (fir >= 10)
	name.insert(8, sec);
	else	
	name.insert(7, sec);	
	return name.toString();
	}		

	}



This post has been edited by kr4vk0dr: 17 April 2010 - 05:54 PM

Was This Post Helpful? 0
  • +
  • -

#9 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: ArrayList to File //only copies part

Posted 17 April 2010 - 06:06 PM

Try:

...
for (String s: L) {
    Put.println(s);         
    Put.flush();
}
...


Was This Post Helpful? 1
  • +
  • -

#10 kr4vk0dr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 25-October 09

Re: ArrayList to File //only copies part

Posted 19 April 2010 - 01:07 PM

View Postn8wxs, on 17 April 2010 - 05:06 PM, said:

Try:

...
for (String s: L) {
    Put.println(s);         
    Put.flush();
}
...



I was skeptical that this would work, but it did! This is the second time in a row you've saved my hide n8wxs thanks again.
Was This Post Helpful? 0
  • +
  • -

#11 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: ArrayList to File //only copies part

Posted 19 April 2010 - 09:57 PM

The PrintWriterconstructor you used does not flush it's output by default.

See Creates a new PrintWriter, without automatic line flushing,
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1