aspose file tools*
The moose likes Beginning Java and the fly likes split text file into bytesize chunks! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "split text file into bytesize chunks!" Watch "split text file into bytesize chunks!" New topic
Author

split text file into bytesize chunks!

Kina H-son
Greenhorn

Joined: Sep 13, 2005
Posts: 2
Hi people!
I've been at this for hours, the API is starting to send me bats, time to ask the experts...

My goal is to in the end send parts of a text file as byte arrays to mimic a file transfer program,
so I thought a good start would be to read the textfile and cut it into strings of a certain size.
Split() seems handy, since it puts them in a string array, or tokenizer, but how do I set the delimiter to simply be a size, say 254 chars (1016 bytes right?)?
Or is there a way to insert another delimeter in a interval, eg ":" after every n-th char?
Or can this be done straight into a byte array using getBytes() and specify the size of the array alements?
Sorry to put three questions in one, as You can guess it's for some silly school project, but I'll be alright if I can just get a push in the right direction..
Thanks!
Satish Chilukuri
Ranch Hand

Joined: Jun 23, 2005
Posts: 266
There's no need to go for strings. Here's a simple way to do it:

Kina H-son
Greenhorn

Joined: Sep 13, 2005
Posts: 2
Jihooo!
Thanks, You're a star!
I have two questions though before I can use this code:
will it set the whole byte array to CHUNK_SIZE or the array elements?
(the elements would be most handy)
What is (b)?
love K.
Satish Chilukuri
Ranch Hand

Joined: Jun 23, 2005
Posts: 266
What is (b)?


Oops.. sorry. A typo. It should be the array of byte "chunk"

will it set the whole byte array to CHUNK_SIZE or the array elements?


I didn't quite get you. It will create a byte array of length CHUNK_SIZE. Each element of the array will be of the type "byte". In java, a byte literal represents a signed integer of 8 bits. So if CHUNK_SIZE is 128, using the array, you can read 128 bytes at a time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: split text file into bytesize chunks!