4 Replies Latest reply: Mar 25, 2008 3:43 AM by 807601 RSS

    Split a text and write different lines in different text file

    807601
      Hello, I am new in java, I am working on a project, but I will start with a simple question (I hope it is simple, cause for me is not). How do I split a text and write each line in a different text file? Here is what I've started:

      import java.io.*;
      import java.util.*;
      
      class test 
      {
           
           public test() throws IOException 
           
           {
                   readAdd("test.txt"); // so here I read the text
                                                  // and then I have to create the output files
      
                     String outfiles = "out";
                   for (int i=1; i<4; i++)     
                    {
                        write(outfiles+String.valueOf(i)+".txt");
                    }   
                   write("out.txt");
                          
           }
           
           private void readAdd(String name) throws IOException //the read function
           {
                   BufferedReader reader = new BufferedReader(new FileReader(name));
                   String line,all=new String(); 
                   while ((line = reader.readLine ()) != null)
                        {
                                all+=line+'\n';
                        } 
                   reader.close ();// so I read all the text
                   
           }
           
           private void write(String name) throws IOException  //and the write function
                                                                                                 // but how do I say this line goes in out1.txt this line goes in out2.txt and so on...
           {
                   BufferedWriter writer = new BufferedWriter(new FileWriter(name));
                   String s=new String();
           
                             
                   
                   writer.write(s);
                   writer.close();
           }
           
           public static void main(String[] args) throws IOException 
           {
                   new test();
           }
      }
      At least help me with some comments on my code.
      Thanks a lot
        • 1. Re: Split a text and write different lines in different text file
          807601
          You'll need a Writer for each file you want to write to. Then when writing, have a loop that reads a line at a time and writes to the correct file.
          String currentLine;
          int currentFile = 0;
          while ((currentLine = inFile.readLine()) != null) {
              //write currentLine to currentFile
          
              currentFile++;
              if (currentFile == numberOfFiles)
                  currentFile = 0;
          }
          • 2. Re: Split a text and write different lines in different text file
            807601
            I think I need more help or please recomand what to read. Ireally appreciate you help.
            Here is what I've tried but I don't really have experience in Java
            Thanks.
            import java.io.*;
            import java.util.*;
             
            class test 
            {
                 public test() throws IOException 
                 
                 {
                         readAdd("test.txt"); // so here I read the text
                                                        // and then I have to create the output files
             
                           String outfiles = "out";
                            for (int i=1; i<4; i++)     
                             {
                                 write(outfiles+String.valueOf(i)+".txt");
                             }   
                         write("out.txt");
                                
                 }
                 
                 private void readAdd(String name) throws IOException //the read function
                 {
                         BufferedReader reader = new BufferedReader(new FileReader(name));
                         String line,all=new String(); 
                         while ((line = reader.readLine ()) != null)
                              {
                                      all+=line+'\n';
                              } 
                         reader.close ();// so I read all the text
                         
                         
                                      
                         BufferedWriter inFile = new BufferedWriter(new FileWriter(name));
                         
                         String currentLine;
                         int currentFile = 0;
                         while ((currentLine = inFile.readLine()) != null) {
                             //write currentLine to currentFile
                          
                             currentFile++;
                             if (currentFile == numberOfFiles)
                                 currentFile = 0;
                         }
                         
                 }
                 
            
                 private void write(String name) throws IOException  //and the write function
                                                                                                       // but how do I say this line goes in out1.txt this line goes in out2.txt and so on...
                 {
                         BufferedWriter writer = new BufferedWriter(new FileWriter(name));
                         String s=new String();
                 
                                   
                        writer.write(s);
                         writer.close();
                 }
                 
                 public static void main(String[] args) throws IOException 
                 {
                         new test();
                 }
            }
            • 3. Re: Split a text and write different lines in different text file
              807601
              First off, you need to declare "String all" at the class level, so that your write() method knows about it.

              Say you have this as a class-level variable (declare it outside of any method):
              BufferedWriter [] writer = new BufferedWriter[4];
              then in your test() method for loop, you add:
              for (int i=1; i<4; i++)     
              {
                  String fileName = outfiles + String.valueOf(i) + ".txt";
                  BufferedWriter[i-1] = new BufferedWriter(new FileWriter(fileName));
                  //write(outfiles+String.valueOf(i)+".txt");
              }  
              So now you have a writer for each output file. So in your write() method, you can read from "all" until you find a "\n", and write that to the first writer. Then write the next part to the second writer, and so on.
              • 4. Re: Split a text and write different lines in different text file
                807601
                Hello ... sorry for being so insistent ... I really need to make this program work, I am still reading Java stuff, I am trying to do my best but because of my low knowledges in Java I advance very hard. I got stuck on newark's advices "So in your write() method, you can read from "all" until you find a "\n", and write that to the first writer." All I have till now is just a mess. Any advice will be real welcome.
                import java.io.*;
                import java.util.*;
                 
                class test 
                {
                     
                 BufferedWriter [] writer = new BufferedWriter[4];
                     
                     public test() throws IOException 
                     {
                             readAdd("test.txt"); 
                            
                               String outfiles = "out";
                               for (int i=1; i<4; i++)     
                              
                               {
                                   String fileName = outfiles + String.valueOf(i) + ".txt";
                                   BufferedWriter[i-1] = new BufferedWriter(new FileWriter(fileName));
                                   //write(outfiles+String.valueOf(i)+".txt");
                              }
                     }
                     
                      private void readAdd(String name) throws IOException //the read function
                          {
                                  BufferedReader reader = new BufferedReader(new FileReader(name));
                                  String line,all=new String(); 
                                  while ((line = reader.readLine ()) != null)
                                       {
                                               all+=line+'\n';
                                       } 
                                  reader.close ();
                                  
                          }
                     
                      
                      public  static final char eof   = '\uffff';
                      private static final int empty = '\ufffe';      
                      private static boolean done; // true if recent operation was successful
                      private static char buf;     // last read character
                      private static char[] LS;    // line separator (eol)
                
                      private static char charAfterWhiteSpace() {
                        char c;
                        do c = read(); while (done && c <= ' ');
                        return c;
                      }      
                      
                      
                private void write(String name) throws IOException  
                                                                                                          
                     {
                             BufferedWriter writer = new BufferedWriter(new FileWriter(name));
                             String s=new String();
                     
                             
                                    StringBuffer b = new StringBuffer();
                                    char c = read();
                                    while (done && c != LS[0]) {
                                      b.append(c);
                                      c = read();
                                    }
                
                                    int i = 0;
                                    while (c == LS) {
                          ++i;
                          if (i >= LS.length) { break; }
                          c = read();
                          }

                          if (i < LS.length) {
                          buf = c;
                          } else {
                          buf = empty;
                          }
                          if (b.length() > 0) done = true;
                          return b.toString();
                          
                     
                     
                     
                     String line,all=new String();
                     while ((line = writer.readLine ()) != null)
                          {
                          all+=line+'\n';
                          }
                     
                     String currentLine;
                     int currentFile = 0;
                     while ((currentLine = writer.readLine()) != null) {
                     //write currentLine to currentFile
                     
                     currentFile++;
                     if (currentFile == numberOfFiles)
                     currentFile = 0;
                     }     
                     
                writer.write(s);
                     writer.close();
                     }
                     
                     public static void main(String[] args) throws IOException
                     {
                     new test();
                     }
                }

                Thank you very much!