Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Split a text and write different lines in different text file

807601Mar 21 2008 — edited Mar 25 2008
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 22 2008
Added on Mar 21 2008
4 comments
555 views