6 Replies Latest reply: Oct 8, 2007 11:06 AM by 807600 RSS

    File Input

    807600
      Maybe someone can clarify,
      I am looking for examples where a program allows me to input data from a file,
      http://chortle.ccsu.edu/CS151/Notes/chap22/ch22_8.html

      on the above webpage there is a good explanation, but it's still not clear when I am writing a program how to fit this together. Should I write a program to create the file first? And what if I want to send the output to another file?
      import java.io.*;
      class AddTwo
      {
        public static void main ( String[] args ) throws IOException
        {
          int numberA, numberB;
      
          String line;
          BufferedReader stdin = new BufferedReader( 
              new InputStreamReader( System.in ) );
      
          System.out.println("Enter first number:");
          line      = stdin.readLine();
          numberA   = Integer.parseInt( line );
      
          System.out.println("Enter second number:");
          line      = stdin.readLine();
          numberB   = Integer.parseInt( line );
      
          System.out.println( "Sum: " + (numberA + numberB) );
        }
      }
        • 1. Re: File Input
          807600
          import java.io.*;
          class AddTwo
          {
            public static void main ( String[] args ) throws IOException
            {
              int numberA, numberB;
          
              String line;
              BufferedReader stdin = new BufferedReader( 
                  new InputStreamReader( System.in ) );
          
              System.out.println("Enter first number:");
              line      = stdin.readLine();
              numberA   = Integer.parseInt( line );
          
              System.out.println("Enter second number:");
              line      = stdin.readLine();
              numberB   = Integer.parseInt( line );
          
              System.out.println( "Sum: " + (numberA + numberB) );
            }
          }
          • 2. Re: File Input
            807600
            Where is the file? What are you trying to do?

            You need to be more clear in your question. I see no use, or attempt at reading a file.
            • 3. Re: File Input
              807600
              My question is how to fit these together into a program that does what I want it to,
              it's not clear to me how to produce a program that inputs the data from the file.

              On the next page the text gives the following, but it does not explain how to:
              create a program with the input.txt file or another type of file
              how to read Line 1 A * B, Line 2 A + B
              how to output the data
              with a GUI or without
              how to output the data to another file?

              I want to enter A * B, C *D, and then calculate totals.
              with a file, rather than entering all the data by hand.

              The next page shows the following:
              Here is an input file for use with this program. The file is named input.txt.

              12
              7
              Here is a sample run of the program with this data file:

              C:\users\default\JavaLessons>java AddTwo < input.txt

              Enter first number:
              Enter second number:
              Sum: 19

              C:\users\default\JavaLessons>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
              • 4. Re: File Input
                807600
                C:\users\default\JavaLessons>java AddTwo < input.txt
                
                Enter first number:
                Enter second number:
                Sum: 19
                
                C:\users\default\JavaLessons>
                • 5. Re: File Input
                  807600
                  What is not clear is how the program fits together or why. It would be helpful if there was an analysis of the problem so I could show you what I am trying to do. I can't do that without seeing an example but I do know how to write programs to do most of what I want, but I can't get a program to read a file.
                  • 6. Re: File Input
                    807600
                    hi,
                    take a look at java.io.FileReader instead of InputStreamReader. Check the javadoc for details...
                    cheers