The code in post #20 reads bytes from the file into an int variable and tests if that variable has a -1 value. The statements need to be changed around to be used in a for loop instead of a while...
Type: Posts; User: Norm
The code in post #20 reads bytes from the file into an int variable and tests if that variable has a -1 value. The statements need to be changed around to be used in a for loop instead of a while...
From post #20
fis.read() // reads a byte from file
Inside the loop are these 3 steps:
read next byte
if byte is -1 quit -> the file is too short
print the byte
if (i != -1) {
The loop control varialbe: i will never be -1. It will count...
Post the code with the for loop that won't work.
The steps the code should take for step 1:
loop 4 times
read next byte
if byte is -1 quit -> the file is too short
print the byte
end loop
The advice is the same as the last 3 or 4 times: use a for loop to read the first 4 bytes from the file.
Get rid of the while() loop!
Try writing the method in several steps:
1) write a loop to...
That should be a small method that reads the first few bytes of a file and returns them.
Same answer as above.
Where is the small method you have been working on to do this? All the code...
Do you know how to write a for loop that will loop 4 times? Try using that kind of a loop to read and reformat the bytes that are read from the file instead of a while loop that goes until the end...
Where is the for loop?
Can you isolate the code like the code in post#3 and post it here?
Please post the code and explain what the problem with it was.
Please explain what problem you are having? What does: from a directory" mean?
Are you having problems getting the path to the folder that contains the file?
Do you want the user to select it? ...
If a while loop that reads to the end of the file works for all the bytes in a file, replacing the while loop with a for loop can limit the number of bytes read and displayed.
Change the while loop to a for loop that loops 4 times.
Please edit your post and wrap your code with
<YOUR CODE HERE>
to get highlighting and preserve formatting.
It should be easy to stop reading on any given byte. Can you explain what the problem is with limiting the number of bytes read and displayed?