java StringBuffer question
- x-factor11
-
x-factor11
- Member since: Mar. 24, 2005
- Offline.
-
- Forum Stats
- Member
- Level 11
- Artist
hey guys, workin in java, but i'v hit a snag
trying to take a text file with words in it, and everything in the file into one string.
looks like this
[[code]]public static String getFile(String fileName) throws IOException
{
BufferedReader inFile = new BufferedReader(new FileReader(fileName));
String fileFiller = "";
do
{
fileFiller += inFile.readLine();
fileFiller += "\n";
} while((fileFiller != null));
System.out.println(fileFiller);
inFile.close();
return fileFiller;
[[/code]]
but when I run it, no matter what's in the text file, I get a outOfMemory exception. What's going on here :O
- x-factor11
-
x-factor11
- Member since: Mar. 24, 2005
- Offline.
-
- Forum Stats
- Member
- Level 11
- Artist
also sorry for screwing up the code block a little haha, wrong code command thing, whoops
but uhh still, any help :>!
- x-factor11
-
x-factor11
- Member since: Mar. 24, 2005
- Offline.
-
- Forum Stats
- Member
- Level 11
- Artist
in theory it looks like it should work no problem, but maybe im missing something really obvious here
- x-factor11
-
x-factor11
- Member since: Mar. 24, 2005
- Offline.
-
- Forum Stats
- Member
- Level 11
- Artist
- Sterance
-
Sterance
- Member since: Jul. 24, 2007
- Offline.
-
- Forum Stats
- Member
- Level 23
- Programmer
gotta be patient, this thread moves really slowly. if you take a peek at the java api (you should have this bookmarked and always look here btw) you will find this page. my guess is that the string you are building is getting to big, too fast for the jvm. How big is the text file you are reading in? also, how much memory are you starting the jvm with?
why exactly do you need a massive string with all the lines? hope that helps
Fear Popo Bawa!!!
John Goodman | ¥
while(<>){chomp;print;}
- x-factor11
-
x-factor11
- Member since: Mar. 24, 2005
- Offline.
-
- Forum Stats
- Member
- Level 11
- Artist
At 10/18/11 09:10 PM, Sterance wrote: gotta be patient, this thread moves really slowly. if you take a peek at the java api (you should have this bookmarked and always look here btw) you will find this page. my guess is that the string you are building is getting to big, too fast for the jvm. How big is the text file you are reading in? also, how much memory are you starting the jvm with?
why exactly do you need a massive string with all the lines? hope that helps
oh sorry, I don't come around the programming forums often (or really newgrounds in general these days), so I didn't know how fast or slow responses usually is. I'm usually accustomed to the general forum's fast response haha.
The text file is only like 200 words, but iv tested it with one that is only 5 words long as well, and the error is the same. The string im making is used with other classes to count the number of words/syllables/sentences and gather some other information within the string, but it needs to be a string to go into these classes.
Earlier I had the code set up where it was semi working, but it would count every OTHER line within the text file, because I had t use the readFile(); function twice, but when I only use it once I keep getting the same error.
- Sterance
-
Sterance
- Member since: Jul. 24, 2007
- Offline.
-
- Forum Stats
- Member
- Level 23
- Programmer
I just remembered that all of those "+=" become String.concats. this can be very bad. the memory and time it takes goes up exponentially with each line. if you use StringBuilder you can then use append and some other nifty functions. this should fix your memory problem.
if it doesn't fix your problem i'd try googling more about reading a file into a string. also, you can always learn perl and do it in 3 lines of code!
Fear Popo Bawa!!!
John Goodman | ¥
while(<>){chomp;print;}
- x-factor11
-
x-factor11
- Member since: Mar. 24, 2005
- Offline.
-
- Forum Stats
- Member
- Level 11
- Artist
At 10/18/11 10:09 PM, Sterance wrote: I just remembered that all of those "+=" become String.concats. this can be very bad. the memory and time it takes goes up exponentially with each line. if you use StringBuilder you can then use append and some other nifty functions. this should fix your memory problem.
if it doesn't fix your problem i'd try googling more about reading a file into a string. also, you can always learn perl and do it in 3 lines of code!
yeah I gotta use java though haha, this whole thing is more or less a practice using multiple classes. I did try using a StringBuffer before, but StringBuilder might have different properties, so ill give it a go
- x-factor11
-
x-factor11
- Member since: Mar. 24, 2005
- Offline.
-
- Forum Stats
- Member
- Level 11
- Artist
nah, no luck
what it does before the error though is that the program runs, but nothing happens for about two minutes, if that helps any
- Sterance
-
Sterance
- Member since: Jul. 24, 2007
- Offline.
-
- Forum Stats
- Member
- Level 23
- Programmer
paste the code you are using when you said StringBuilder didn't work (in place of using the += operator)
Fear Popo Bawa!!!
John Goodman | ¥
while(<>){chomp;print;}
- x-factor11
-
x-factor11
- Member since: Mar. 24, 2005
- Offline.
-
- Forum Stats
- Member
- Level 11
- Artist
At 10/18/11 10:31 PM, Sterance wrote: paste the code you are using when you said StringBuilder didn't work (in place of using the += operator)
public static String getFile(String fileName) throws IOException
{
BufferedReader inFile = new BufferedReader(new FileReader(fileName));
String fileFiller = inFile.readLine();
StringBuilder wordHolder = new StringBuilder();
do
{
wordHolder.append(inFile.readLine());
} while((fileFiller != null));
System.out.println(fileFiller);
inFile.close();
return stringHolder;
} - x-factor11
-
x-factor11
- Member since: Mar. 24, 2005
- Offline.
-
- Forum Stats
- Member
- Level 11
- Artist
stringHolder is wordHolder, just didnt change that yet haha
- Sterance
-
Sterance
- Member since: Jul. 24, 2007
- Offline.
-
- Forum Stats
- Member
- Level 23
- Programmer
i just went and coded up what i meant. notice i did not use the "+=" operator. Also i have used Scanner because i think it pretty sweet. I tested this with hamlet and it worked super fast. The "hasNextLine" runs very slightly slower than a do/while loop but its hardly noticeable. hope that helps
public static String getFile(String filename) throws IOException
{
Scanner fileIn = new Scanner(new FileReader(filename));
StringBuilder sb = new StringBuilder();
while(fileIn.hasNextLine())
{
sb.append(fileIn.nextLine());
sb.append("\n"); //nextLine does not return the newline
}
return sb.toString();
} Fear Popo Bawa!!!
John Goodman | ¥
while(<>){chomp;print;}
- x-factor11
-
x-factor11
- Member since: Mar. 24, 2005
- Offline.
-
- Forum Stats
- Member
- Level 11
- Artist
At 10/18/11 11:01 PM, Sterance wrote: i just went and coded up what i meant. notice i did not use the "+=" operator. Also i have used Scanner because i think it pretty sweet. I tested this with hamlet and it worked super fast. The "hasNextLine" runs very slightly slower than a do/while loop but its hardly noticeable. hope that helps
public static String getFile(String filename) throws IOException
{
Scanner fileIn = new Scanner(new FileReader(filename));
StringBuilder sb = new StringBuilder();
while(fileIn.hasNextLine())
{
sb.append(fileIn.nextLine());
sb.append("\n"); //nextLine does not return the newline
}
return sb.toString();
}
that worked absolutely perfect haha.
I guess what was mostly the problem was the BufferedReader class, but what is the difference between Scanner and BufferedReader that would make this all suddenly work?
- Sterance
-
Sterance
- Member since: Jul. 24, 2007
- Offline.
-
- Forum Stats
- Member
- Level 23
- Programmer
you can do it with StringBuilder too (code below). I think your code didn't work the second time because you only converted part of your code. When you are changing class types to try a different it is sometimes best to just comment out all of your current code, and start from scratch (it was only like 6 lines of code so doesn't take long). If you don't start over, you might carry over your bug. In your case, it looks like you were filling your string builder while checking to see if the wrong variable became null (so it just kept filling forever until you get a crash). Anyway, here is my code for StringBuilder. also, i forgot to close my file point in my previous example; always close your filepointers (i just forget in java sometimes).
public static String getFile(String filename) throws IOException
{
BufferedReader fileIn = new BufferedReader(new FileReader(filename));
StringBuilder sb = new StringBuilder();
int buff;
while((buff = fileIn.read())!= -1) //read one at a time. -1 means EOF
sb.append((char)buff); //going from file to buff to sb
fileIn.close();
return sb.toString();
} Fear Popo Bawa!!!
John Goodman | ¥
while(<>){chomp;print;}
- gumOnShoe
-
gumOnShoe
- Member since: May. 29, 2004
- Offline.
-
- Send Private Message
- Browse All Posts (15,244)
- Block
-
- Forum Stats
- Member
- Level 15
- Blank Slate
At 10/18/11 05:16 PM, x-factor11 wrote: BufferedReader inFile = new BufferedReader(new FileReader(fileName));
String fileFiller = "";:
do
{
fileFiller += inFile.readLine();
fileFiller += "\n";
} while((fileFiller != null));
this is wrong regardless. I suggest this loop instead, though your problem will make sense in a moment.
String nextLine;
String fileFiller = "";
while((nextLine = inFile.readLine()) != null) {
fileFiller += nextLine + "\n";
}
Now, the reason you are getting out of memory errors is because you have an infinite loop.
: fileFiller += inFile.readLine();
fileFiller in your code is always the previous block of text + the next line (eventually this looks lie: "thisisallthetextofmyfilenullnullnullnul lnullnullnullnullnullnullnullnullnullnul lnullnullnullnullnullnull...") As you append nulls to the end of the string, the string still has a value.
: } while((fileFiller != null));
hence, fileFiller never equals null, the string gets huge and eventually takes up all of the memory allocated to the java heap.
Read into separate string. Test that string before trying to work with it. And then append it to a different buffer.
Regards,
gum


