Results 1 to 4 of 4
Thread: reading a file problem
- 08-03-2011, 05:18 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 66
- Rep Power
- 0
reading a file problem
I'm still learning java so this will likely be an obvious fix, sorry in advance :P
So, I'm trying to load strings from a file into a string array. Then return that array. Here's my "attempt code":
Java Code:public String [] loadFile (String fileName){ int i = 0; int j = 0; try{ FileInputStream fstream = new FileInputStream(fileName); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); while (br.readLine() != null) { i++; } in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } String sentences[] = new String [i]; try{ FileInputStream fstream = new FileInputStream(fileName); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); while ( br.readLine() != null) { String temp = "a"; temp = br.readLine(); sentences [j] = temp; j++; } in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } return sentences; }
Alex left.
It is written exactly like that but also includes an invisible "\n".
Basically my problem is that the readLine method returns null.
I ran a debugger and here is what stumps me, the while loop condition requires readLine() to be different from null. However when the Temp variable receives value of readLine() it is null
Should you need it here is the code that saves data to the file:
Java Code:public void SaveToFile (String sentence, String fileName){ try { BufferedWriter out = new BufferedWriter(new FileWriter(fileName)); out.write(sentence); out.close(); } catch (IOException e) { } }
Thanks in advance :)
- 08-03-2011, 05:38 AM #2
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
The bolded statements are the problem. You are advancing the buffer two lines for every iteration of the loop. You need to save the string that is returned by the readline() method within the stopping condition of your while loop
Java Code:String line; while ( (line = br.readLine()) != null){ //reference line variable instead of calling readLine() again }
Last edited by yellowledbet; 08-03-2011 at 05:54 AM.
- 08-03-2011, 06:02 AM #3
Also, it is inefficient to read the file twice. You should just read it once. Which raises the problem of how big to make the array. You have 2 options.
1. Make a really big array that can fit all your data. This is also inefficient if you create an array to hold 100,000 Strings but only store 2 Strings.
1.a) To avoid the above problem you can make a small array and "grow" it as needed. By this I mean create an array with a size or say 10. Then when it gets full and you haven't finished, make another larger array and copy all your data across.
2. A better solution is to add the Strings to a List. Then when you have finished reading the file convert the List to an array.
- 08-03-2011, 08:22 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 66
- Rep Power
- 0
Similar Threads
-
Reading from file problem
By BillyB in forum New To JavaReplies: 10Last Post: 03-18-2011, 05:09 AM -
problem reading file
By jmoutia in forum New To JavaReplies: 0Last Post: 10-31-2010, 03:19 AM -
Problem reading from a file
By sarapeace in forum New To JavaReplies: 13Last Post: 10-03-2010, 03:08 PM -
Reg: File Reading Problem
By balaji csc in forum New To JavaReplies: 0Last Post: 11-06-2009, 05:22 PM -
Problem with reading text from a .txt file
By Gigi in forum New To JavaReplies: 40Last Post: 01-22-2009, 04:22 AM
Bookmarks