This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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!
There's no need to go for strings. Here's a simple way to do it:
Joined: Sep 13, 2005
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.
Joined: Jun 23, 2005
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.