• Post Reply Bookmark Topic Watch Topic
  • New Topic

Breaking up files, please help!  RSS feed

 
rosin tuck
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i need help with a simple I/O problem. I have a large text file, which i want to break up into smaller files. I want to break the file each time "From -" is found at the start of a new line.


public static void main(String[] args) throws Exception {

String path1 = "file1";

BufferedReader br = new BufferedReader( new InputStreamReader(new FileInputStream(path1)));

BufferedWriter out = new BufferedWriter(new FileWriter("Emails//out.txt"));
String line;
while((line = br.readLine()) != null)
{
if(line.startsWith("From -")){

//send from here to the next "from -" into a text file


Above is as far as ive come, i dont know how to send each block of text to an individual file, can anybody help, it would be greatly appreciated!
Thanks
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you already have a BufferedWriter object called 'out'. Looking at the javadocs of BufferedWriter, what methods are there that you could use to write a line of text? If there aren't any good methods in BufferedWriter, maybe there is something in a superclass of it?
 
Sameer Damle
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is another way

read the entire file data using FileInputStream into a byte array.

make a String out of that byte array.

split the string around "form -".

you will get the array of strings splitted around "form -".

then you can write the indivisual string into seperate file using FileOutputStream.

hope this will help you.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you comfortable with the parts Ulf brought up? Once you can read and write lines with ease, let's restate your problem as simply as possible in pseudo code:

What would "stop writing current file" and "start writing next file" look like in File methods? I made a couple assumptions ... the first line is not a - and we don't want to write the - to any output file. What would you change if I guessed wrong?
[ January 27, 2006: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!