• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

split text file into bytesize chunks!

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic