output file - BufferedWriter

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

    #16
    Phew..I've typed wrong in my program.
    just finished deleted 27000 created output files in my dir. In the input file - there are 159451lines.
    kindly please help me to figure it out ....really appreciate any help. thank you
    Code:
    public class Transfer
    {    
         private static String lineFRST = "";
         
         public static void main(String[] args) throws IOException
        {           
             BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));
             spool(inputFIRST);
        }
           
     public static void spool(BufferedReader in) throws IOException 
     { 
     BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));       
         
           PrintWriter printWriter = null;    
           BufferedWriter bufferWriter = null;
           
           int fileno = 1;  
           lineFRST = inputFIRST.readLine();   
           
           while(lineFRST != null) 
           {
             if (lineFRST.indexOf("groupAStart") >= 0)
             {
                 if (printWriter == null)
                 makeOutFile (fileno++);
             }
             else if (printWriter != null)
             {
                 printWriter.println(lineFRST);  
                 if (lineFRST.indexOf("groupAFinish") >=0)
                 {
                     printWriter.close();
                     printWriter = null;
                 }
             }   
           }
     }
                 
       public static void makeOutFile(int fileno) throws IOException 
       {
           PrintWriter printWriter = null;    
           BufferedWriter bufferWriter = null;
           bufferWriter = new BufferedWriter( new FileWriter("output" +fileno +".txt", true));   //this not works...     
                      
            String[] thelineFRST = lineFRST.split(" ");
            String num1 = thelineFRST[0];
            String num2 = thelineFRST[1];
            String method = thelineFRST[2];        
             
            bufferWriter.write(num1+ " " + num2+ "\n");                  
            bufferWriter.close();            
        }  
    }

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #17
      Originally posted by shana07
      Phew..I've typed wrong in my program.
      just finished deleted 27000 created output files in my dir. In the input file - there are 159451lines.
      kindly please help me to figure it out ....really appreciate any help. thank you
      Code:
      public class Transfer
      {    
           private static String lineFRST = "";
           
           public static void main(String[] args) throws IOException
          {           
               BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));
               spool(inputFIRST);
          }
             
       public static void spool(BufferedReader in) throws IOException 
       { 
       BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));       
           
             PrintWriter printWriter = null;    
             BufferedWriter bufferWriter = null;
             
             int fileno = 1;  
             lineFRST = inputFIRST.readLine();   
             
             while(lineFRST != null) 
             {
               if (lineFRST.indexOf("groupAStart") >= 0)
               {
                   if (printWriter == null)
                   makeOutFile (fileno++);
               }
               else if (printWriter != null)
               {
                   printWriter.println(lineFRST);  
                   if (lineFRST.indexOf("groupAFinish") >=0)
                   {
                       printWriter.close();
                       printWriter = null;
                   }
               }   
             }
       }
                   
         public static void makeOutFile(int fileno) throws IOException 
         {
             PrintWriter printWriter = null;    
             BufferedWriter bufferWriter = null;
             bufferWriter = new BufferedWriter( new FileWriter("output" +fileno +".txt", true));   //this not works...     
                        
              String[] thelineFRST = lineFRST.split(" ");
              String num1 = thelineFRST[0];
              String num2 = thelineFRST[1];
              String method = thelineFRST[2];        
               
              bufferWriter.write(num1+ " " + num2+ "\n");                  
              bufferWriter.close();            
          }  
      }
      I don't understand what you're doing here:

      1) you create a BufferedReader in you main() method, you pass it to the spool()
      method and there you ignore it and open another BufferedReader. Why?

      2) I my suggested code the makeOutFile method was just supposed to create
      and return a PrintWriter; you decided to make it a void method. Why?

      3) You write something in the makeOutFile method. Why?

      4) Note that the printWriter is always null because you never assign something
      to that variable? Why not?

      kind regards,

      Jos

      Comment

      • shana07
        Contributor
        • Jan 2007
        • 280

        #18
        Originally posted by JosAH
        I don't understand what you're doing here:

        1) you create a BufferedReader in you main() method, you pass it to the spool()
        method and there you ignore it and open another BufferedReader. Why?

        2) I my suggested code the makeOutFile method was just supposed to create
        and return a PrintWriter; you decided to make it a void method. Why?

        3) You write something in the makeOutFile method. Why?

        4) Note that the printWriter is always null because you never assign something
        to that variable? Why not?

        kind regards,

        Jos
        I managed to solve it Josah - by your advice of cause (after got so many Why Qs from you... :))
        I am learning a lot from you.thank you very much

        Comment

        Working...