5 Replies Latest reply: Apr 9, 2010 8:28 AM by 843789 RSS

    BufferedReader .readLine always null...  Help

    843789
      Hello,
      I was making a class that would make writing and reading to a file very easy. I know how to use the reader and scanner but i thought if I made my own class then it would be so much easier... Anyway, I am trying to make it to where I can write to a certain line or read a certain line by reading the first few characters to a line, telling if the number is the number in the method argument, and then either reading the entire line or writing to that line... I can write the numbers to the line so that it looks like 1>> 2>> 3>> with a different number on each line, but i can't get it to read the lines to do any of the other stuff... So here is my code:
      In my class I have:
              //declare variables
           public int line = 1;
           public String spacer = ">>";
           public String file;
           public FileReader fileReader;
           public BufferedReader read;
           public FileOutputStream outStream;
           public PrintWriter write;
           //construct the modifier
           public FileMod (String filename) throws Exception {
                file = filename;
           }
           //initialize and close the modifier
           void initModder () throws Exception {
                fileReader = new FileReader(file);
                read = new BufferedReader (fileReader);
                outStream = new FileOutputStream(file);
                write = new PrintWriter(outStream);
           }
           void closeModder () throws Exception {
                write.flush();
                write.close();
                outStream.close();
                read.close();
                fileReader.close();
           }
           //read a line
           void readLine (int lineNumber)throws Exception {
                String lineRead;
                int lineReadLength;
                int startspace = 0;
                int endspace = 0;
                int lineReadInt;
                
                do {
                     lineRead = read.readLine();
                     lineReadLength = lineRead.length();
                     switch (lineReadLength) {
                          case 3 : 
                               endspace = 1;
                               break;
                          case 4 :
                               endspace = 2;
                               break;
                          case 5 :
                               endspace = 3;
                               break;
                          case 6 :
                               endspace = 4;
                               break;
                          case 7 :
                               endspace = 5;
                               break;
                          case 8 :
                               endspace = 6;
                     }
                     lineReadInt = Integer.parseInt((lineRead.substring(startspace, endspace)));
                }
                while (lineReadInt != lineNumber - 1);
                String fullLineRead = read.readLine();
                System.out.println(fullLineRead);
           }
           void readTest () throws Exception{
                String lineRead = read.readLine();
                System.out.println(lineRead);
           }
      And on my main class i have:
      public static void main(String[] args) throws Exception {
                FileMod fileMod = new FileMod("./src/pkg/file.zzz");
                fileMod.initModder();
                fileMod.addLine(100);
                fileMod.readTest();
                fileMod.closeModder();     
           }
      I can add the lines easily and control the amount, but i can't read them. I tried using a Scanner and that didn't work, then i tried this Buffered Reader way and it still isn't working. **The read test thing was just a simple reader that i had so that i could check if it would even read a pre-input line and it still didn't work.

      I have no idea where the issue is and any help is appreciated!
      Thanks!
      Thimbletack

      I am really sorry for posting this in this forum... i was in a different forum and it posted it in here... I can't figure out how to take it down so... Sorry again!
        • 1. Re: BufferedReader .readLine always null...  Help
          699554
          Side stepping your question to shed some light on your code:

          The variable spacer appears to be a constant and should be declared as so:
          public String spacer = ">>";
          public static final String spacer = ">>";
          When closing readers and writers...
          void closeModder () throws Exception {
                    write.flush();
                    write.close();
                    outStream.close();
                    read.close();
                    fileReader.close();
          }
          ... you only need to close the outer streams, as they will in turn close the element to which they are wrapped around. Also explicility calling flush() is usually not necessary before calling close(), as close() implicitly calls flush() in all cases that i'm aware of.
          //read a line
          void readLine (int lineNumber) throws Exception 
          Avoid writing pointless comments. If a method is named readLine, why stress the point that the method "reads a line". Comments should be used to, in the initial sentence of the comment accurately describe the purpose and functionality of the method. And in further sentences extend upon this idea. In this comment shred some light as to how you read a line in your application. E.g. how it differs from normally reading a line from a text file.
          switch (lineReadLength) {
                              case 3 : 
                                   endspace = 1;
                                   break;
                              case 4 :
                                   endspace = 2;
                                   break;
                              case 5 :
                                   endspace = 3;
                                   break;
                              case 6 :
                                   endspace = 4;
                                   break;
                              case 7 :
                                   endspace = 5;
                                   break;
                              case 8 :
                                   endspace = 6;
          }
          Cases 3-8 are merely magic numbers in my eyes, they have no meaning whats so ever. I am unable to determine what each case handles. To improve the quality and readability of your code, use comments or express each case as a descriptive constant.
          while (lineReadInt != lineNumber - 1);
          
          String fullLineRead = read.readLine();
          System.out.println(fullLineRead);
          The while loop does absolutely nothing, were the condition of the while loop to be true then you would have an infinite loop. This is because a a semicolon is present at the end of the while loop, therefore the condition of the while loop can and will never change. If you intended to include the following two lines, incorporating them into the while loop, then make sure to use curly brackets.

          Mel
          • 2. Re: BufferedReader .readLine always null...  Help
            843789
            Thanks for the help on that stuff... I think that I put a little bit too much on my plate at first... My main issue is that my reader isn't reading, it is just deleting all of the data within the file. So the main code even at it's most simple state isn't working and is returning null. Here is the code I made just to read whatever is in the file.
            public String file;
                 public FileInputStream inStream;
                 public InputStreamReader inReader;
                 public BufferedReader read;
                 public FileOutputStream outStream;
                 public PrintWriter write;
                 
                 public FileMod (String filename) throws Exception {
                      file = filename;
                 }
                 void initModder () throws Exception {
                      inStream = new FileInputStream (file);
                      inReader = new InputStreamReader(inStream);
                      read = new BufferedReader(inReader);
                      outStream = new FileOutputStream(file);
                      write = new PrintWriter(outStream);
                 }
                 void closeModder () throws Exception {
                      write.close();
                      read.close();
                 }
                    void readTest () throws Exception{
                      String lineRead = read.readLine();
                      System.out.println(lineRead);
                 }
            And here is what i have on my main class:
                 public static void main(String[] args) throws Exception {
                      FileMod fileMod = new FileMod("./src/pkg/file.zzz");
                      fileMod.initModder();
                      fileMod.readTest();
                      fileMod.closeModder();     
                 }
            Everything else i shouldn't have really included...
            It is giving me the value of null whenever it reads the file. Even if I put the text in before it will somehow delete all of the text in the file before actually reading it.

            Anyone know why this is happening?
            • 3. Re: BufferedReader .readLine always null...  Help
              YoungWinston
              Thimbletack wrote:
              Hello,
              I was making a class that would make writing and reading to a file very easy. I know how to use the reader and scanner but i thought if I made my own class then it would be so much easier...
              Not a bad start
              Anyway, I am trying to make it to where I can write to a certain line
              Have you looked at LineNumberReader?
              ...I tried using a Scanner and that didn't work...
              I'm not an expert here, but from what I understand, Scanner is meant for keyboard-type stuff and may be very slow for files.

              Are you trying to "edit" a file? Because, if so, and it's a text file, you'd be far better off to read it all in, do your "edits", and then overwrite it when you're done. Random-access updates with an open file generally aren't a good idea. A file is NOT a database.

              Winston
              • 4. Re: BufferedReader .readLine always null...  Help
                EJP
                My main issue is that my reader isn't reading, it is just deleting all of the data within the file.
                No it isn't. The new FileWriter() is doing that. You can't read from a file and create a new one with the same name at the same time. It doesn't even make sense. Create the output file with a new name, and at the end delete and rename as required.
                • 5. Re: BufferedReader .readLine always null...  Help
                  843789
                  Hi

                  I cannot see the looping which should take place, how have you sorted - up this problem?

                  How about the node object? You'll have to declare it to attach the file to the BufferedReader.

                  cheers



                  Adriana

                  NB. Java Development Fundamentals Theory and Practice

                  http://www.fortemall.com/auction_details.php?name=Development-Fundamentals-Theory-and-Practice&auction_id=11568

                  Edited by: bambola on Apr 9, 2010 6:27 AM