Bixiangdong _ Java basic video tutorial 19th _ IO stream (11 ~ 12), _ java_io

Source: Internet
Author: User

Bixiangdong _ Java basic video tutorial 19th _ IO stream (11 ~ 12), _ java_io
19th-11-IO stream (byte stream File read/write operations)

Import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException;/* bytes stream Reader/WriterFileReader/FileWriterBufferedReader/BufferedWriter byte stream: InputStream/OutputStreamFileInputStream/FileOutputStream requirement: You want to operate image data, in this case, the byte stream */public class FileStream {public static void main (String [] args) throws IOException {// writeFile (); readFile_2 ();} is used ();} public static void writeFile () throws IOEx Ception {FileOutputStream fos = new FileOutputStream ("fos.txt"); fos. write ("abc powerful ". getBytes (); // write close requires no need to flush -- the bottom layer of the upstreaming stream is also one byte and one byte for operation, but it needs to read several bytes and then check the code output characters, therefore, cache and flush are involved. the byte stream does not need to be cached and does not need flush fos. close ();} public static void readFile_0 () throws IOException {FileInputStream FD = new FileInputStream ("fos.txt"); // read int ch in one byte; while (ch = FCM. read ())! =-1) {System. out. println (char) ch);} fiis. close ();} public static void readFile_1 () throws IOException {FileInputStream FD = new FileInputStream ("fos.txt"); byte [] buf = new byte [1024]; // 1024 * N is the proper size of the byte array. int len; while (len = Fi. read (buf ))! =-1) {System. out. println (new String (buf, 0, len, "UTF-8"); System. out. println (new String (buf, 0, len);} FCM. close ();} // available () method specific to byte stream public static void readFile_2 () throws IOException {FileInputStream FD = new FileInputStream ("fos.txt "); // int available () returns the method called next time for this input stream "The estimated number of remaining bytes that can be read (or skipped) from 'this input stream' without blocking" (including line breaks) // if a new byte with the same size as the number of remaining file bytes. available. however, when the file size is large, the byte [] request for memory will fail. available ()]; FCM. read (buf); System. out. println (new String (buf); Sox. close ();}}
19th days-12-IO stream (copying images)
Import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException;/* copy an image. Idea: 1. use byte to read stream objects and associate them with source images. create an image file by writing bytes to the stream object to store the obtained image data. 3. data storage is completed through cyclic read/write. close Resources */public class CopyPic {public static void main (String [] args) {FileOutputStream fos = null; FileInputStream FD = null; try {fos = new FileOutputStream ("2.png "); FCM = new FileInputStream ("1.png"); byte [] buf = new byte [1024 ]; Int len; while (len = Fi. read (buf ))! =-1) {fos. write (buf, 0, len) ;}} catch (Exception e) {throw new RuntimeException ("failed to copy the file") ;}finally {try {if (FS! = Null) FCM. close ();} catch (IOException e) {throw new RuntimeException ("failed to close the read stream");} try {if (fos! = Null) fos. close () ;}catch (IOException e) {throw new RuntimeException ("failed to close the output stream ");}}}}

Q: Can a ghost stream be used for image copying?

A: No. The data read by the hacker stream. If no corresponding number is found in the code table, A number corresponding to an unknown character is returned. The unknown character occupies one byte. Similarly, if a byte stream truncates characters by mistake, it may also cause garbled characters.

 

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.