ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
This works, but what worries me is that I can not figure out what will happen when I read a massive big text file. Somewhere memory will get exhausted and an exception should be trown. But I can't find that exception.
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.
This works, but what worries me is that I can not figure out what will happen when I read a massive big text file. Somewhere memory will get exhausted and an exception should be trown. But I can't find that exception.
Can somebody advice.
For a large file, you don't want to concatenate onto the end of a string as you are doing -- it's very inefficient and for large files the slowdown becomes dramatic (Java has to find the string ending on each append operation). Instead, append each line to a StringBuffer instance. Then, after the file is completely read, assign the StringBuffer result to a string. Much faster and more efficient.
This is a mistake:
Code:
FileData += "\n";
Always use the platform line ending in a Java program. Remember that Java programs are supposed to be platform-portable.
For truly huge files, you may need to launch your program with a larger memory allotment:
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?
Last edited by Wim Sturkenboom; 10-10-2009 at 08:29 AM..
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.
In that case, why not read the entire file, not line by line as you are doing? If your intention is to get an internal representation of the exact content of the target file, you aren't going about it right.
Here is one example (somewhat out of date, but just to show the idea):
Code:
public String readFile(String path) throws Exception
{
File f = new File(path);
char[] buff = new char[(int)f.length()];
FileReader fin = new FileReader(f);
fin.read(buff);
fin.close();
return new String(buff);
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.