Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

split text file into bytesize chunks!

 
Kina H-son
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no need to go for strings. Here's a simple way to do it:

 
Kina H-son
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic