I/O File problem..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • withu4ever
    New Member
    • Dec 2007
    • 4

    I/O File problem..

    hi all,
    I'm using Java input /out files
    I'm reading from a txt file which holds intger
    and I would like to sum them and put the result in a new txt file holds the output as an integer but it holds un-understandable thing such as ( )

    this is the source code , it's compiled,

    Code:
    package filetest2;
    import java.util.Scanner;
    import java.io.*;
    
    
    class fileTest2  {
        public static void main(String [] args) throws Exception{ 
    
            FileInputStream a1 = new FileInputStream("input.txt") ;
            DataInputStream a2 = new DataInputStream(a1) ;
            
            FileOutputStream a3 = new FileOutputStream("out.txt");
            DataOutputStream a4 = new DataOutputStream(a3);
            
            Scanner scan = new Scanner (a1);
             int sum1 = 0;
                  
            int numOfLines = scan.nextInt();
                for (int i= numOfLines;i>0;i--)
                {  int numOfTests = scan.nextInt();
                        for (int j = numOfTests;j>0;j--)
                           sum1 =+  scan.nextInt();
                  
                }
            
                a4.write(sum1);
                
                a3.close();
                a1.close();
            
            }
    }
    Waiting for your respone..
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    It's perfectly understandable if you can read binary data. You're mixing up too
    many streams:

    - get rid of stream a2, you're not using it.
    - make a4 a PrintStream and make it println(sum), not write(sum) it.
    - read the API docs for those streams.

    kind regards,

    Jos

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      Originally posted by withu4ever
      hi all,
      I'm using Java input /out files
      I'm reading from a txt file which holds intger
      and I would like to sum them and put the result in a new txt file holds the output as an integer but it holds un-understandable thing such as ( )

      this is the source code , it's compiled,

      Code:
      package filetest2;
      import java.util.Scanner;
      import java.io.*;
      
      
      class fileTest2  {
          public static void main(String [] args) throws Exception{ 
      
              FileInputStream a1 = new FileInputStream("input.txt") ;
              DataInputStream a2 = new DataInputStream(a1) ;
              
              FileOutputStream a3 = new FileOutputStream("out.txt");
              DataOutputStream a4 = new DataOutputStream(a3);
              
              Scanner scan = new Scanner (a1);
               int sum1 = 0;
                    
              int numOfLines = scan.nextInt();
                  for (int i= numOfLines;i>0;i--)
                  {  int numOfTests = scan.nextInt();
                          for (int j = numOfTests;j>0;j--)
                             sum1 =+  scan.nextInt();
                    
                  }
              
                  a4.write(sum1);
                  
                  a3.close();
                  a1.close();
              
              }
      }
      Waiting for your respone..
      At first, you have reversed the shorthand arithmetic operator (+=)....
      @ sum1 =+ scan.nextInt();

      If you like to have an experiment on capturing values in text file...
      You can also use Scanner class...

      Update us,
      sukatoa

      Comment

      • withu4ever
        New Member
        • Dec 2007
        • 4

        #4
        Thanks for ur respond,
        I'll try it now again :)

        Comment

        • withu4ever
          New Member
          • Dec 2007
          • 4

          #5
          Originally posted by JosAH
          It's perfectly understandable if you can read binary data. You're mixing up too
          many streams:

          - get rid of stream a2, you're not using it.
          - make a4 a PrintStream and make it println(sum), not write(sum) it.
          - read the API docs for those streams.

          kind regards,

          Jos
          I made what you have said,
          It WORKS :D :D :D
          Thanks a Lot

          Comment

          Working...