Java and String
Note in advance: I'm coming from a C world and this is my first Java stuff.
I have the following piece of code to read a ascii file; FileData is of type String. Code:
public static boolean ReadFile(File infile) { Can somebody advice. |
If the JVM cannot allocate any more memory, you'll get an OutOfMemoryError exception thrown. It's classified as an unchecked exception, which means any method can throw it without declaring that it throws it.
But you really don't want that to happen, because after that exception is thrown then nothing else can really happen. Also, you really don't want to be using Strings like that. In Java, strings are immutable - they don't change. What actually happens in your program is that FileData gets allocated twice per loop. Firstly, memory gets allocated for FileData that is the current length of FileData data plus the length of the new line, then the data is copied across, then the existing memory is marked as free so that the garbage collected can free it. Secondly, memory gets allocated for FileData that is the current length of FileData plus the length of "\n", then the data is copied across, then the existing memory is marked as free so that the garbage collected can free it. If the garbage collected doesn't run quickly enough, then you're going to run out of memory far sooner than you would expect. Use StringBuffer for this kind of thing, it doesn't re-allocate memory like String does. The reason for String behaving like this is so that the JVM can efficiently share String instances across your application - i.e. if you have these Strings: "fred" "freddie" "freddiejohn" and "john" then Java actually holds this as "freddiejohn" with pointers and length counts to define the four actual strings involved. |
Quote:
This is a mistake: Code:
FileData += "\n"; For truly huge files, you may need to launch your program with a larger memory allotment: Code:
$ java -Xmx1000m ProgramName |
Thanks to both.
OK, I will read up on StringBuffer. I have two problems with nextLine. 1) It reads including the line terminator but returns the string without it. When displaying the data in a textarea, it is one long line if I do not manually insert a line terminator. 2) Secondly it ignores empty lines at the end of the textfile by the looks of it and therefore does not give a true reflection of the file. Because of this, I'm actually looking for a class and method that can read a file in one go (but have not found it yet exactly). I did find some other classes (http://java.sun.com/docs/books/tutorial/essential/io/) that I must look at and probably first determine the filesize and next use a read. Any hints for this? |
Quote:
Here is one example (somewhat out of date, but just to show the idea): Code:
public String readFile(String path) throws Exception |
Thanks lutusp,
your reply came slightly too late. I did rework my code after some more research and it now looks like this. Code:
/* |
All times are GMT -5. The time now is 03:18 AM. |