DJDonaL3000
DJDonaL3000

Reputation: 15

Java readFile from byte x to byte y

Im trying to read a text file, x bytes at a time into a variable, process each "chunk" and write it to another file.

So far all I can do sucessfully is:

// read each line at a time:
while ( ( str = in.readLine() ) != null){ ....

Is it possible to specify something like... str = in.readBytes( 320 up to 400 ) ... ???

Any thoughts or comments welcome.

Upvotes: 0

Views: 405

Answers (1)

Ben S
Ben S

Reputation: 69382

What you're looking for is read(byte[]).

So you would something like:

InputStream in; // initialized however you're doing
int bytesRead = 0;
byte bytes[60]; // We'll read in up to 60 bytes at a time
while ((bytesRead = in.read(bytes)) > 0) {
    // bytesRead is = to the number of bytes actually read
    // bytes array holds the next 'bytesRead' number of bytes
}

Upvotes: 4

Related Questions