Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

the while loop in IO stream  RSS feed

 
xue haipeng
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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?
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
xue haipeng
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!