Operation of Java io on files

Source: Internet
Author: User

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
    1. Import Java.io.File;
    2. Import Java.io.FileInputStream;
    3. Import java.io.FileNotFoundException;
    4. Import Java.io.FileOutputStream;
    5. Import java.io.IOException;
    6. Import Junit.framework.TestCase;
    7. Public class Testbinarystreamreadwrite extends TestCase {
    8. /* 
    9. * 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,
    10. * The first three bytes are discarded, only the last byte is stored
    11. */
    12. public void Testcreatebytefile () {
    13. try {
    14. FileOutputStream fo = new FileOutputStream ("e:/tmp/tmp");
    15. byte B =-1;
    16. //write two to the file-1, no characters encoded as-1, so the file created is a pure binary file
    17. Fo.write (b);
    18. Fo.write (b);
    19. Fo.close ();
    20. } catch (FileNotFoundException e) {
    21. E.printstacktrace ();
    22. } catch (IOException e) {
    23. E.printstacktrace ();
    24. }
    25. }
    26. /* 
    27. * 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
    28. * 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
    29. * 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,
    30. * 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
    31. * Change to byte type, or read by using read (Byte b).
    32. */
    33. public void Testreadencodeerror () {
    34. try {
    35. FileInputStream fi = new FileInputStream ("e:/tmp/tmp");
    36. 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
    37. ///end without available Bytes, return value -1
    38. int readInt = Fi.read ();
    39. //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,
    40. //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
    41. While (readInt! =-1) {
    42. //Input two x 255, but the input encoding is wrong, should be 1
    43. System.out.println (READINT);
    44. //Read Next byte
    45. ReadInt = Fi.read ();
    46. }
    47. Fi.close ();
    48. } catch (FileNotFoundException e) {
    49. E.printstacktrace ();
    50. } catch (IOException e) {
    51. E.printstacktrace ();
    52. }
    53. }
    54. /* 
    55. * Code read with Inputstream.read if the byte type is strongly converted, we can no longer use ReadByte
    56. *! =-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
    57. * is a binary file, you can not directly use the real code and 1 comparison, can only use available or pass Inputstream.read
    58. * (byte[] b) method returns the number of bytes read to determine
    59. */
    60. public void Testavailable () {
    61. try {
    62. File File = new file ("e:/tmp/tmp");
    63. FileInputStream fi = new FileInputStream (file);
    64. System.out.println ("available is essentially the same as file length:" +file.length ());
    65. (Byte) fi.read () cannot be used after the encoding read from the Read method has been strongly transferred to byte! =- to
    66. //judgment is whether the file stream is over, here we can only use available to determine whether the end of the file
    67. While (fi.available () > 0) {
    68. System.out.println ("available=" + fi.available ());
    69. ///Use the Read () method to read the encoding of the byte content, only strong to byte type to get the true encoding
    70. System.out.println ((byte) fi.read ());
    71. }
    72. Fi.close ();
    73. } catch (FileNotFoundException e) {
    74. E.printstacktrace ();
    75. } catch (IOException e) {
    76. E.printstacktrace ();
    77. }
    78. }
    79. /* 
    80. * When reading binary files (slices) instead of character files, use InputStream in addition to reading () to read the strong-to-byte type.
    81. * Read (byte[] b) is the best way to use read the number of bytes returned to determine whether to finish reading
    82. */
    83. public void Testreadbybytearr () {
    84. try {
    85. FileInputStream fi = new FileInputStream ("e:/tmp/tmp");
    86. byte[] Bytearr = new byte[1024];
    87. //Number of bytes read
    88. int readcount = Fi.read (Bytearr);
    89. //If the end of the file has been reached, return-1
    90. While (readcount! =-1) {
    91. For (int i = 0; i < Readcount; i++) {
    92. System.out.println (Bytearr[i]);
    93. }
    94. Readcount = Fi.read (Bytearr);
    95. }
    96. Fi.close ();
    97. } catch (FileNotFoundException e) {
    98. E.printstacktrace ();
    99. } catch (IOException e) {
    100. E.printstacktrace ();
    101. }
    102. }
    103. /* 
    104. * Use Inputstream.read to read binary file strongly convert byte type to get true encoding, cannot use readbyte! =
    105. *-to determine whether to the end of the file
    106. */
    107. public void Testgetbytecodebyconvernt () {
    108. try {
    109. FileInputStream fi = new FileInputStream ("e:/tmp/tmp");
    110. //Read a byte content, force to byte type, get the true encoding
    111. byte ReadByte = (byte) fi.read ();
    112. //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
    113. While (readbyte! =-1) {
    114. System.out.println ((byte) readbyte);
    115. ReadByte = (byte) fi.read ();
    116. }
    117. Fi.close ();
    118. } catch (FileNotFoundException e) {
    119. E.printstacktrace ();
    120. } catch (IOException e) {
    121. E.printstacktrace ();
    122. }
    123. }
    124. }

Operation of Java io on files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.