Results 1 to 4 of 4
  1. #1
    Maikl is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Default 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

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    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..

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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:

    Java Code:
    String line = null;
    while ((line = yourBufferedReader.readLine()) != null) {
        // do stuff
    }
    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.

Similar Threads

  1. Replies: 5
    Last Post: 02-02-2010, 10:06 AM
  2. FileReader help
    By emp in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 05:41 AM
  3. add FileReader to GUI
    By VinTiger in forum New To Java
    Replies: 8
    Last Post: 05-11-2009, 01:23 AM
  4. FileReader / Buffered Reader
    By sepaht in forum New To Java
    Replies: 9
    Last Post: 07-10-2008, 09:05 PM
  5. Help with filereader in java
    By zoe in forum Advanced Java
    Replies: 2
    Last Post: 07-26-2007, 10:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •