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