Forums Register Login

the while loop in IO stream

+Pie Number of slices to send: Send


Like this code segment, the while loop is very frequently used in java IO to read/write stream Objects, but I don't quite understand the loop:

1. the Java API says the write(int) method is to "write the specified byte to this file output stream." but it still confused me that if the OutputStream object and the InputStream object are automatically connected? So if temp bytes are read, then we can use os.write(temp) to write the input content?

2. the while loop executes several times, does the write() operation override the previous content? Or just appends the new read bytes?
1
+Pie Number of slices to send: Send
 

the while loop is very frequently used in java IO to read/write stream Objects, but I don't quite understand the loop



Lets break it down a little bit. The loop that you posted is in fact the same as this one:



In the read() method specification we read that:

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.

So in comment 1, we read the next byte from the input file. In comment 2, we check to see if the byte is -1 or not. If it is, this means that we have reached the end of the file, so there is no need to continue the loop (see comment 3). If the byte we read is not -1, then we write it to the output stream that we have (in our case, a file). As you can see, both loops do the same job, only the one I am showing you now, does it with more lines of code.

if the OutputStream object and the InputStream object are automatically connected



They are not connected. You simply read "something" from one stream and give to the other. This "something" could be a byte, a table of bytes (see method read(byte[] b) ), or even a String (see method readLine() in BufferedReader).

So if temp bytes are read, then we can use os.write(temp) to write the input content?



Exactly. In some cases you may need to close the streams first before you read and/or write, but yes, this is correct.

the while loop executes several times, does the write() operation override the previous content? Or just appends the new read bytes?



It overrides the previous content. If you want to append data to the end of the file, you will have to use the constructor of the FileOutputStream that accepts a boolean as a parameter, which indicates whether you want to append data or not. See here.

I hope the above clarified things for you a little bit.
+Pie Number of slices to send: Send
 

Panagiotis Kalogeropoulos wrote:
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255.


Thanks,

Each time the while loop is excuted, the read() method reads a byte to the input stream, and returns the in value of the byte, but here I'm confused what the value represents? I print it out and find that if Arabic numeral or latin alphabet is read, the return value is coincidentally the ASCII value of the read byte, and if a non-Latin character is read, each chatacter corresponds to two integer value, both are greater than 128 and less than 256. I wonder if the return value is related to specific coding scheme of the opetating system, or it is the regulation of Java ?

Many thanks!
+Pie Number of slices to send: Send
That depends on the charset which was used to encode the data. It appears that the charset in your case might have been UTF-8, but there are other possibilities. Note that Java uses Unicode internally to represent characters, and it uses a variety of charsets to convert between the internal (Unicode) representation in chars and the external (text file) representation in bytes.
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3937 times.
Similar Threads
regarding peer to peer network through java
Doubt in HTTPServletResponse
Transferring file name then file data over socket
Cut the file into 32kb pieces
Problem creating the zip file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:17:01.