Results 1 to 4 of 4
Thread: FileReader problems
- 12-13-2009, 04:07 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 1
- Rep Power
- 0
FileReader problems
Here's the deal. I have a program that creates a file. I don't want to go in detail about what the file actually contains (but I will have to do if you can't solve my problem). The problem is, I want to be able to read the file many times because I have to do the average of some values contained in the file.
Now i'm fairly new to java so there might be an easier way to do this. I used a "BufferedReader fr = new BufferedReader(new FileReader(fileName))" and i read each line with fr.readLine()
I have put this inside of a loop thats supposed to run through the whole file looking for specific values. What I tried to do is reinitialize the FileReader to line 1 by creating a new BufferedReader each time.. seems like it's not working. Here's a that part of my code (variables and comments are in french :():
public static double moyenne() throws IOException
{
String nomFichier = "F:\\Users\\Mike\\Documents\\Michael's Documents\\Test" + test + "_" + date + ".txt";
String ligne = "";
String[] tab1 = new String[5];
String separateur = ";";
double valeur1 = 0;
double valeur2 = 0;
double total = 0;
int compteur = 1;
BufferedReader lecteurFichier = new BufferedReader(new FileReader(nomFichier));//nouveau buffered reader pour recommencer chaque fois(?)
lecteurFichier.readLine();//pour ne pas inclure les 4 premieres lignes
lecteurFichier.readLine();
lecteurFichier.readLine();
lecteurFichier.readLine();
while (ligne != null) //lire tout le fichier
{
ligne = lecteurFichier.readLine();//lire ligne par ligne
tab1 = ligne.split(separateur);// metter dans un tableau
if (tab1.length != 5) //la derniere ligne est toujours incomplete. On fait ca pour que le programme ne plante pas
{
ligne = null;
}
valeur1 = Double.parseDouble(tab1[0]);//valeur de la longueur
if (valeur1 == temp3 || valeur1 == (temp3 + interLong)) //si la valeur de la longueur correspond a l'intervalle
{
compteur++;//pour faire la moyenne
valeur2 = Double.parseDouble(tab1[3]);//la vitesse
total += valeur2;//on additionne toutes les vitesses
}
}
return total / compteur;
}
public static void graphique()
{
for (temp3 = LongMin; temp3 <= LongMax; temp3 += (2*interLong))//interLong = Long/19
{
moyenne = moyenne();
}
}
Any help is appreciated.
Thank you
- 12-13-2009, 06:53 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
I am from sweden and I still write my own code in english with english comments
I suggest you do that too. that way we got it way more easier to read your script..
-
Myself, I would read the file only once and place the information into a collection such as an arraylist.
- 12-14-2009, 11:51 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
Two things. You aren't closing your BufferedReader.
Should be done in a finally block.
That could be the cause of yor problems (not sure there).
Secondly your while loop's not quite correct.
This is the more normal usage:
Your way results in you having to process a line even if it doesn't actually exist...surprised you don't get a null pointer exception from that to be honest.Java Code:String line = null; while ((line = yourBufferedReader.readLine()) != null) { // do stuff }
Similar Threads
-
FileReader Vs FileInputStream and same goes to output
By unhurt in forum New To JavaReplies: 5Last Post: 02-02-2010, 09:06 AM -
FileReader help
By emp in forum New To JavaReplies: 1Last Post: 07-28-2009, 04:41 AM -
add FileReader to GUI
By VinTiger in forum New To JavaReplies: 8Last Post: 05-11-2009, 12:23 AM -
FileReader / Buffered Reader
By sepaht in forum New To JavaReplies: 9Last Post: 07-10-2008, 08:05 PM -
Help with filereader in java
By zoe in forum Advanced JavaReplies: 2Last Post: 07-26-2007, 09:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks