output file - BufferedWriter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    output file - BufferedWriter

    Good day!
    Kindly please someone tells me why my program generates same content of output file? They're supposed to have different contents.
    Must be something wrong about my codes..help me to figure it out, Thank you very much...
    Code:
    for(int i=0; i < 5; i++)    {        
    try { 
        output = new BufferedWriter( new FileWriter(FILENAME + ind + ".txt", true)); 
        output.write(name+ " " + add+ " " + phone + "\n"); 
        output.close(); 
         }catch (IOException e) 
          {
           System.out.println("There was a problem creating/writing to the temp file");
            e.printStackTrace();
          } 
     }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shana07
    Good day!
    Kindly please someone tells me why my program generates same content of output file? They're supposed to have different contents.
    Must be something wrong about my codes..help me to figure it out, Thank you very much...
    Code:
    for(int i=0; i < 5; i++) { 
    try { 
    output = new BufferedWriter( new FileWriter(FILENAME + ind + ".txt", true)); 
    output.write(name+ " " + add+ " " + phone + "\n"); 
    output.close(); 
    }catch (IOException e) 
    {
    System.out.println("There was a problem creating/writing to the temp file");
    e.printStackTrace();
    } 
    }
    What have you done in your code to make your output different?

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #3
      Originally posted by r035198x
      What have you done in your code to make your output different?
      I put output.close(); outside try..
      still the same...

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by shana07
        I put output.close(); outside try..
        still the same...
        Shana, you are using a for loop to output to the file and the part of code you've posted has nothing to suggest that the output should be different.

        Comment

        • shana07
          Contributor
          • Jan 2007
          • 280

          #5
          Originally posted by r035198x
          Shana, you are using a for loop to output to the file and the part of code you've posted has nothing to suggest that the output should be different.
          Ok r035198x..hope you can leave this message for a while.
          I need to solve this first, please ...
          Code:
          output.write(number + "\n");           
          output.write ("A:" +name);
          prints output:
          51 A
          How to make it go to next line like this:
          51
          A

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by shana07
            Ok r035198x..hope you can leave this message for a while.
            I need to solve this first, please ...
            Code:
            output.write(number + "\n");           
            output.write ("A:" +name);
            prints output:
            51 A
            How to make it go to next line like this:
            51
            A
            Wrap a PrintWriter around your BufferedWriter and use two separate println()s
            for the fields.

            kind regards,

            Jos

            Comment

            • shana07
              Contributor
              • Jan 2007
              • 280

              #7
              Originally posted by JosAH
              Wrap a PrintWriter around your BufferedWriter and use two separate println()s
              for the fields.

              kind regards,

              Jos
              Got it Josah!
              Thank you very much.

              Comment

              • shana07
                Contributor
                • Jan 2007
                • 280

                #8
                Allow me to ask one more question about array syntax please.. please
                take a look at codes below: Thank you.
                Code:
                 for(int ind=0; ind < 10; ind += 2)  
                        {
                             b1 = arrayArgs[ind];
                             b2 = arrayArgs[ind+1];         
                           
                            switch (opcode) 
                             {           
                                case GCD:       out = b1.gcd(b2).toString();
                                break;
                                default:        throw new BigAlException("invalid operation");
                             }  
                       String[] output = {out}; //this part - how to initiliaze out to an output array?  
                     }
                       System.out.println(output); //so here I can print all of outputs from the array

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by shana07
                  Allow me to ask one more question about array syntax please.. please
                  take a look at codes below: Thank you.
                  Code:
                   for(int ind=0; ind < 10; ind += 2)  
                          {
                               b1 = arrayArgs[ind];
                               b2 = arrayArgs[ind+1];         
                             
                              switch (opcode) 
                               {           
                                  case GCD:       out = b1.gcd(b2).toString();
                                  break;
                                  default:        throw new BigAlException("invalid operation");
                               }  
                         String[] output = {out}; //this part - how to initiliaze out to an output array?  
                       }
                         System.out.println(output); //so here I can print all of outputs from the array
                  "out" is an int (I guess), so your array initialization should look like this:
                  Code:
                  String output= { ""+out };
                  kind regards,

                  Jos

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by shana07
                    Allow me to ask one more question about array syntax please.. please
                    take a look at codes below: Thank you.
                    Code:
                     for(int ind=0; ind < 10; ind += 2)  
                            {
                                 b1 = arrayArgs[ind];
                                 b2 = arrayArgs[ind+1];         
                               
                                switch (opcode) 
                                 {           
                                    case GCD:       out = b1.gcd(b2).toString();
                                    break;
                                    default:        throw new BigAlException("invalid operation");
                                 }  
                           String[] output = {out}; //this part - how to initiliaze out to an output array?  
                         }
                           System.out.println(output); //so here I can print all of outputs from the array
                    "out" is an int (I guess), so your array initialization should look like this:
                    Code:
                    String output= { ""+out };
                    The expression ""+out has type String so it can be an element of a String array.

                    kind regards,

                    Jos

                    Comment

                    • shana07
                      Contributor
                      • Jan 2007
                      • 280

                      #11
                      I am stucked now on how to separate my txt file content. I need to print out them using BufferedWriter method....

                      I have these data in a txt file:

                      2 0 groupAStart
                      2 1 groupAStart //From here
                      3 2 james
                      5 3 jun
                      3 4 groupAfinish
                      8 5 groupAfinish //Until here - goes to output1.txt
                      3 6 junk
                      5 7 junk
                      2 8 groupAStart //from here
                      2 9 groupAStart
                      3 10 jello
                      5 11 juniady
                      2 12 groupAfinish
                      2 13 groupAfinish // Until here - goes to output2.txt
                      .....
                      .....
                      ....
                      I don't know what is the syntax to make it work. As I have done this........ple ase advise me..Thank you.
                      Code:
                       for(int i=0; i<=100 ; i++)  
                      {           
                        try
                        { 
                          BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));
                                
                        for(int ind=0; ind<=159451;ind++)
                         {
                                      String[] thelineFRST = lineFRST.split(" ");
                                      String pos = thelineFRST[0];
                                      String num = thelineFRST[1];
                                      String group = thelineFRST[2];                      
                                  
                                    if(method.equals(""))  //I am clueless this part
                                    {
                      bufferWriter = new BufferedWriter( new FileWriter("OutputFirst" +i + ".txt", true)); 
                                     bufferWriter.write(pos+ " " +num+ " " + "\n");                  
                                     bufferWriter.close(); 
                                    }
                                }          
                               printWriter.close();   
                               inputFIRST.close();              
                          ....
                      Happy Labour Day!

                      Comment

                      • shana07
                        Contributor
                        • Jan 2007
                        • 280

                        #12
                        Originally posted by JosAH
                        "out" is an int (I guess), so your array initialization should look like this:
                        Code:
                        String output= { ""+out };
                        The expression ""+out has type String so it can be an element of a String array.

                        kind regards,

                        Jos
                        Yes, out is an int (BigInteger), and don't you think that it should look like this
                        Code:
                        String[B][][/B] output= { ""+out };
                        But then, I still print nothing ....erm do you think it's good idea to initiliaze them in into array? Because my idea is to print all the output one short after finish calculating many input values.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by shana07
                          Yes, out is an int (BigInteger), and don't you think that it should look like this
                          Code:
                          String[B][][/B] output= { ""+out };
                          But then, I still print nothing ....erm do you think it's good idea to initiliaze them in into array? Because my idea is to print all the output one short after finish calculating many input values.
                          My bad, I mistyped my answer (lame excuse: I haven't had my espresso yet).
                          You want a String array so it can be constructed as you showed above.

                          This gives you an array with one element in it which is the String representation
                          of variable 'out'. I apologize for the typo.

                          kind regards,

                          Jos

                          Comment

                          • shana07
                            Contributor
                            • Jan 2007
                            • 280

                            #14
                            Originally posted by JosAH
                            My bad, I mistyped my answer (lame excuse: I haven't had my espresso yet).
                            You want a String array so it can be constructed as you showed above.

                            This gives you an array with one element in it which is the String representation
                            of variable 'out'. I apologize for the typo.

                            kind regards,

                            Jos
                            hihiih..no worries ! I know that was a trick to test me ;)
                            I'll try again to make it works
                            Kindly please read my aboved message...I have problem on how to separate file contents.Thanks a bunch...

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by shana07
                              Kindly please read my aboved message...I have problem on how to separate file contents.Thanks a bunch...
                              Basically you have lines; each line contains three strings; when a line contains
                              a string "groupAStar t" you want to start spooling the rest of the text to an
                              OutputStream or Writer until a line contains the String groupAEnd; right?
                              Something like this will get you started:
                              Code:
                              public void spool(BufferedReader in) throws IOException { // read from this reader
                                 int fileno= 1; // file number in "output#.txt"
                                 String line;
                                 PrintWriter out= null; // if null: don't print anything
                                 while ((line= in.readLine()) != null) { // while next line available
                                    if (line.indexOf("groupAStart") >= 0) {
                                       if (out == null)
                                          out= makeOutFile(fileno++); // build a PrintWriter
                                    }
                                    else if (out != null) {
                                       out.println(line);
                                       if (line.indexOf("groupAEnd") >= 0) {
                                          out.close();
                                          out= null;
                                       }
                                 }
                                 // let caller close the in stream
                              }
                              Note that this little methods needs a couple of other methods, e.g. opening
                              a PrintWriter for the output given a serial number and it needs a 'driver' method
                              that opens the BufferedReader, calls this method and closes the reader again.
                              Instead of sticking all functionality in one big, ugly method better decompose
                              the problem like I did in this little example.

                              kind regards,

                              Jos

                              Comment

                              Working...