It's not a secret anymore!
The moose likes I/O and Streams and the fly likes split a file to original files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "split a file to original files" Watch "split a file to original files" New topic

split a file to original files

komaravolu raju

Joined: Oct 25, 2010
Posts: 11
I am writing a program to copy all the file content to a single file and split the single file into the original file.

>> raju.txt
>> document.rtf

these are my 2 files. I am able to mix them successfull, ie., copy the contents of the file to a file >> output.dat file, by adding some identifiers like starting of the file, file path and file name.
now, my issue is that how to split the files into the original file. if given the file name.

I mean to say that, form the output.dat file I want to separate raju.txt and document.txt while deleting or with out deleting the content in the output.dat file.

I wanna do this in byte streams.

This is the file I have written to copy n number of files to output.dat.

this works fine.

now how to split the files???

***Note: Every thing should be in ByteStreams
Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

Welcome to the Ranch!

Could you please UseCodeTags next time? I've added them for you this time.

As for the issue. Your writing algorithm may work, but it makes it harder for you to read the data back. There are no clear separators, that indicate where one part starts and another ends. Sure, you write the file name, but you write it as bytes. That means that you don't know where it ends.

If I were you I'd just use ZipOutputStream / ZipInputStream / ZipFile. That way you get all the hard work for free. If you don't want to, or cannot, you need better separators. Here's how I have written something like this before when this question was asked before:
The reading is then easy enough, using DataInputStream:
- read a UTF string
- read a long
- read exactly that many bytes

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: split a file to original files
It's not a secret anymore!