Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

split a file to original files

 
komaravolu raju
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.
I am writing a program to copy all the file content to a single file and split the single file into the original file.

Eg:
>> 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
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic