Inputstream.read ()
Returns int , with a range of 0 to 255 int values, reading the next data byte from the input stream, which is read in bytes, reading only one byte at a time, reading 0 of the three bytes after the third front, so that the resulting read is always positive. and a number from 0 to 255. returns a value of 1 if no bytes are available because the end of the stream has been reached. Used for reading of the binary file.
If we are reading a binary file, a sound file, we should use the following two ways to read:
The first: or use the Inputstream.read () method to read, but we cast the int type Byte, so that in the process of conversion, the first three bytes will be discarded 0, and finally get the real code read from the stream. But if this is read directly through the Read () method, rather than through read (byte[] b), we determine whether the stream is ending, preferably using the available () method, and, of course, using the direct comparison to read the result is-1, However, it is important to note that we cannot judge after the strong turn to byte after reading, because the binary file may have a code of-1.
The second type: Use Inputstream.read (byte[] b) to receive, because this does not have a byte to int promotion process, byte array b is stored in the real encoding. If Read (byte[] b) reads to the end of the stream, it returns-1, so we directly determine the number of read sub-sections returned to know if the stream is over.
Outputstream.write (int b)
Writes the specified bytes to this output stream. write specifies that a byte is written to the output stream. The byte to write is the eight low of the parameter B. The 24 highs of B will be ignored. This method writes a negative encoding to a file that can be written to a binary stream file, such as a sound, picture, and so on.
Let's look at the corresponding method of reader and writer character Stream:
Reader.read
Reader.read: Reads a single character. This method is blocked until there are available characters, an I/O error occurred, or the end of the stream has been reached. The range is between 0 and 65535 (0X00-0XFFFF), which essentially reads a char type, which is Unicode encoded. Returns 1 if the end of the stream has been reached
Writer. Write (int c)
Writer. Write (int c): writes a single character. The characters to be written are contained in 16 lows of the given integer value, and 16 high are ignored.
From the above can be seen is two kinds of character stream, one is byte stream, the other is a character stream, if we read/write is a binary file, then use byte stream inputstream.read/outputstream.write; if we read/write a character file, It is convenient to use a character stream reader.read/writer.write, which can also be manipulated using a byte stream, but it is not very convenient in some cases.
Java code
- Import Java.io.File;
- Import Java.io.FileInputStream;
- Import java.io.FileNotFoundException;
- Import Java.io.FileOutputStream;
- Import java.io.IOException;
- Import Junit.framework.TestCase;
- Public class Testbinarystreamreadwrite extends TestCase {
- /*
- * When writing binary files (such as sound files), only the byte stream write is used. The Outputstream.write method writes only the lowest eight bits,
- * The first three bytes are discarded, only the last byte is stored
- */
- public void Testcreatebytefile () {
- try {
- FileOutputStream fo = new FileOutputStream ("e:/tmp/tmp");
- byte B =-1;
- //write two to the file-1, no characters encoded as-1, so the file created is a pure binary file
- Fo.write (b);
- Fo.write (b);
- Fo.close ();
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
- /*
- * Inputstream.read method is to read a byte content after the return of the int type, there is a conversion between this process: when reading
- * After taking a byte of content, the eight-bit binary before the 24-bit binary 0, so the last bytes returned is encoded as a positive number, never
- * will be negative, with a range of 0-255 integers. This way, if you read the binary file using the Read method, the encoded encoding is an integer,
- * may have been negative code, the final display as positive encoding. In order to ensure that the correct original binary code is obtained, it can only be forced after reading
- * Change to byte type, or read by using read (Byte b).
- */
- public void Testreadencodeerror () {
- try {
- FileInputStream fi = new FileInputStream ("e:/tmp/tmp");
- The //read side IFC reads a byte content that returns an int byte value in the range of 0 to 255 if the stream has been reached
- ///end without available Bytes, return value -1
- int readInt = Fi.read ();
- //returns 1 if the end of the file has been reached. Although the contents of each byte in the file are 1, but the read out is not-1,
- //So the second byte can also output, without the problem that only the first byte can be read and the second byte cannot be read
- While (readInt! =-1) {
- //Input two x 255, but the input encoding is wrong, should be 1
- System.out.println (READINT);
- //Read Next byte
- ReadInt = Fi.read ();
- }
- Fi.close ();
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
- /*
- * Code read with Inputstream.read if the byte type is strongly converted, we can no longer use ReadByte
- *! =-To determine if the file stream is over, because if it is a binary file, the read-out encoding is negative, in short, if the read
- * is a binary file, you can not directly use the real code and 1 comparison, can only use available or pass Inputstream.read
- * (byte[] b) method returns the number of bytes read to determine
- */
- public void Testavailable () {
- try {
- File File = new file ("e:/tmp/tmp");
- FileInputStream fi = new FileInputStream (file);
- System.out.println ("available is essentially the same as file length:" +file.length ());
- (Byte) fi.read () cannot be used after the encoding read from the Read method has been strongly transferred to byte! =- to
- //judgment is whether the file stream is over, here we can only use available to determine whether the end of the file
- While (fi.available () > 0) {
- System.out.println ("available=" + fi.available ());
- ///Use the Read () method to read the encoding of the byte content, only strong to byte type to get the true encoding
- System.out.println ((byte) fi.read ());
- }
- Fi.close ();
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
- /*
- * When reading binary files (slices) instead of character files, use InputStream in addition to reading () to read the strong-to-byte type.
- * Read (byte[] b) is the best way to use read the number of bytes returned to determine whether to finish reading
- */
- public void Testreadbybytearr () {
- try {
- FileInputStream fi = new FileInputStream ("e:/tmp/tmp");
- byte[] Bytearr = new byte[1024];
- //Number of bytes read
- int readcount = Fi.read (Bytearr);
- //If the end of the file has been reached, return-1
- While (readcount! =-1) {
- For (int i = 0; i < Readcount; i++) {
- System.out.println (Bytearr[i]);
- }
- Readcount = Fi.read (Bytearr);
- }
- Fi.close ();
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
- /*
- * Use Inputstream.read to read binary file strongly convert byte type to get true encoding, cannot use readbyte! =
- *-to determine whether to the end of the file
- */
- public void Testgetbytecodebyconvernt () {
- try {
- FileInputStream fi = new FileInputStream ("e:/tmp/tmp");
- //Read a byte content, force to byte type, get the true encoding
- byte ReadByte = (byte) fi.read ();
- //Here we can not use the following method to determine whether the end of the read stream, because the encoded content itself is-1, so will not output any content
- While (readbyte! =-1) {
- System.out.println ((byte) readbyte);
- ReadByte = (byte) fi.read ();
- }
- Fi.close ();
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
- }
Operation of Java io on files