-
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":
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;
}
Here is the file I'm trying to read from (its content exactly):
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 :s:
Should you need it here is the code that saves data to the file:
Code:
public void SaveToFile (String sentence, String fileName){
try {
BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
out.write(sentence);
out.close();
} catch (IOException e) {
}
}
What am I doing wrong?
Thanks in advance :)
-
Quote:
Originally Posted by
aianta
Code:
while ( [B]br.readLine() [/B]!= null) {
String temp = "a";
[B]temp = br.readLine();[/B]
sentences [j] = temp;
j++;
}
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
Code:
String line;
while ( (line = br.readLine()) != null){
//reference line variable instead of calling readLine() again
}
-
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.
-
Thanks
@yellowledbet
Thanks, that was it. It works perfectly now! :)
@Junky
Thanks for the reply, I had a feeling I was doing some unnecessary steps there. Unfortunately I'm a complete Noob with Java (still learning the basics), I'll look up lists and see what I can do thanks for the hint!