13 Replies Latest reply: Oct 12, 2009 10:40 AM by 843789 RSS

    Read file one more time.

    843789
      pls tell me reason why the second loop it does not show any thing.
      file input like: 1 7 f d
      here the code
      import java.io.File;
      import java.io.FileReader;
      import java.io.FileWriter;
      
      import java.io.IOException;
      
      public class ReadFile 
      {
           
           public static void main(String args[]) throws  IOException
           {
                
                FileReader in = new FileReader(new File("input.txt"));
       
                int c;
                System.out.print("Read first :");
                while((c = in.read()) != -1)
                     System.out.print((char)c + " ");
                
                System.out.print("\nRead second:");
                while((c = in.read()) != -1)
                     System.out.print((char)c + " ");
                
                in.close();
           }
          
      }
      Edited by: how_i_do on Oct 12, 2009 8:26 AM
        • 1. Re: Read file one more time.
          843789
          In your first loop you read until you reach the end of the file.

          In the second loop you read until you reach the end of file.

          You don't re-open the file or otherwise re-set the position in the file.

          Therefore the very first read in the second loop will already return -1 (indicating the end of the input).
          • 2. Re: Read file one more time.
            843789
            and how i reset to read file.
            i use in.reset() between the first and second loop
            while()
            in.reset()
            while()
            but there is an IOException : reset() is not supported
            • 3. Re: Read file one more time.
              843789
              how_i_do wrote:
              but there is an IOException : reset() is not supported
              reset() would be the obvious solution.

              Alternatively you can close the reader (don't forget that step!) and open it again.
              • 4. Re: Read file one more time.
                843789
                how_i_do wrote:
                but there is an IOException : reset() is not supported
                Maybe wrap it in a BufferedReader
                • 5. Re: Read file one more time.
                  843789
                  could you show me your code to do this.
                  i have tried some way like close and reopen,
                  put them in 2 method but it does not work
                  • 6. Re: Read file one more time.
                    843789
                    how_i_do wrote:
                    could you show me your code to do this.
                    i have tried some way like close and reopen,
                    Show us the code that you tried ...
                    put them in 2 method but it does not work
                    ... and define "not work". How exactly does it not work?

                    And please read this and specifically this before answering.
                    • 7. Re: Read file one more time.
                      843789
                      here is my code
                      import java.io.File;
                      import java.io.FileReader;
                      import java.io.FileWriter;
                      
                      import java.io.IOException;
                      
                      public class ReadFile 
                      {
                           private   FileReader in; 
                           public ReadFile()
                           {
                            try{
                                 in = new FileReader(new File("input.txt"));
                                 }catch(IOException e)
                                 {
                                      e.fillInStackTrace();
                                 }
                           }
                           
                           public void readFirst() throws IOException
                           {
                                int c;
                                System.out.print("Read first :");
                                while((c = in.read()) != -1)
                                     System.out.print((char)c + " ");
                                in.close();
                                     
                           }
                           public  void readSecond() throws IOException
                           {
                                int c;
                                System.out.print("Read first :");
                                while((c = in.read()) != -1)
                                     System.out.print((char)c + " ");
                                in.close();
                           } 
                           
                           public static void main(String args[]) throws  IOException
                           {
                                ReadFile readfile = new ReadFile();
                                     readfile.readFirst();
                                     readfile.readSecond();
                           }
                          
                      }
                      • 8. Re: Read file one more time.
                        843789
                        JoachimSauer wrote:
                        how_i_do wrote:
                        i have tried some way like close and reopen,
                        I see that you close the Reader in your close, but where do you re-open it?
                        • 9. Re: Read file one more time.
                          843789
                          i think when i close file in the first method
                          and invoke in second method it will be re-open.
                          • 10. Re: Read file one more time.
                            843789
                            how_i_do wrote:
                            i think when i close file in the first method
                            and invoke in second method it will be re-open.
                            Why do you think that?

                            When in doubt (or when your observations don't match your expectations), then check [the API|http://java.sun.com/javase/6/docs/api/java/io/FileReader.html] to verify your assumptions.

                            Or put differently: you expected it to auto-magically re-open the file. It seems that this is not the case. You will have to do that step explicitly. How did you open the file the first time? Maybe that's how you should open it the second time as well.

                            Edit: and don't confuse things by spreading your code over multiple methods just yet. It is good programming style to separate your code into methods to enhance the structure of your code, but currently it only serves to confuse the issue (because the fact that code is executed in separate methods doesn't influence your current problem at all).
                            • 11. Re: Read file one more time.
                              843789
                              my purpose is the ways to solve the problem i shown in first code.
                              if have show me.
                              • 12. Re: Read file one more time.
                                3004
                                how_i_do wrote:
                                my purpose is the ways to solve the problem i shown in first code.
                                if have show me.
                                You don't get to dictate what kind of help you receive. You have been given good advice. Follow it, and if you don't understand part of it, ask.
                                • 13. Re: Read file one more time.
                                  843789
                                  how_i_do wrote:
                                  my purpose is the ways to solve the problem i shown in first code.
                                  My goal is to show you how to solve your problem.
                                  if have show me.
                                  If you don't want to learn how to solve the problem, then I don't want to help you. Good luck.