Monday, July 26, 2010

Split a File

This is a snippet of a huge project.
What this code does is it monitors a give .txt file.
If the file contains more than 300 lines, it splits the file and repeats this procedure till end of line is reached.


public void split(String master_out)
{
try
{
String str2="";
int t = 1;
System.out.println(master_out);
FileReader input = new FileReader(master_out);
BufferedReader br = new BufferedReader(input);
while(br.ready())
{
File SplitFile=new File(master_out + "_" + (t++) + ".TXT");// Error file validations
PrintWriter writer=new PrintWriter(new FileWriter(SplitFile));
for(int j = 0; j < 3000; ++j)
{
if((str2 = br.readLine()) != null)
writer.println(str2);
else
break;
}
writer.close();
}
}
catch(IOException e)
{
System.out.println(e);
}
catch(Exception e)
{
System.out.println(e);
}
}

}

No comments:

Post a Comment