File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Trouble reading large file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply Bookmark "Trouble reading large file" Watch "Trouble reading large file" New topic
Author

Trouble reading large file

Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
I'm trying to put about half a million strings into a linkedlist.
Im reading using bufferedReader, but it just keeps loading and nothing happens, or at best i get a java heap memory error.

Here's the code i use:



Any idea how i could solve the problem? I need every line to be read since i have to choose a random word for the game.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 3133
No, you don't actually need to read the whole thing into memory at once. Depending on your specific requirements, there are different approaches you could take. However, if you want to start with the simple approach of just reading it all in, then you have two options:

1) Increase the JVM's max available heap at startup time with the -Xmx option. For example, java -Xmx 512m -cp whatever Myclass

2) Start with a smaller file--much, much, much smaller--until you've verified that the logic of your program is correct. This is a good idea regardless of what approach you finally decide on, as it will make testing much simpler.
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
Jeff Verdegan wrote:No, you don't actually need to read the whole thing into memory at once. Depending on your specific requirements, there are different approaches you could take. However, if you want to start with the simple approach of just reading it all in, then you have two options:

1) Increase the JVM's max available heap at startup time with the -Xmx option. For example, java -Xmx 512m -cp whatever Myclass

2) Start with a smaller file--much, much, much smaller--until you've verified that the logic of your program is correct. This is a good idea regardless of what approach you finally decide on, as it will make testing much simpler.


The program works perfect if i use a list with lets say 20 words. It's a hangman game, but the wordlist have like 400k + rows. A part of my task is to encrypt and decrypt all words in the file (seperate program).

But then in the game i need to read in all words, choose a random word for the game, decrypt it and present it to the player.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 13842

Johannes Thorén wrote:The program works perfect if i use a list with lets say 20 words.


I'm surprised. It looks to me like you're only putting half of the words into the list. But I suppose that testing wouldn't really expose that sort of error, since selecting random items from half of the words is almost indistinguishable from selecting random items from all of the words.
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
Paul Clapham wrote:
Johannes Thorén wrote:The program works perfect if i use a list with lets say 20 words.


I'm surprised. It looks to me like you're only putting half of the words into the list. But I suppose that testing wouldn't really expose that sort of error, since selecting random items from half of the words is almost indistinguishable from selecting random items from all of the words.


Yeah i think its more of an iterationproblem. Changed the code to this:



Since i only need one word for the game, i thought of reading just one word from the file. (altho i'm going to have to deal with the problem of encrypting every word on the file later). But it still just keeps loading and loading.>
 
IntelliJ Java IDE
 
subject: Trouble reading large file
 
Threads others viewed
URL Connection Servlet help
Create a "virtual file"
Executing dir from Java
How to solve NullpointerException
How to ask for user input twice?
developer file tools