Java IO stream copying image and javaio Stream Image
(1) Copying images using byte streams
1 // byte stream method 2 public static void copyFile () throws IOException {3 4 // 1. obtain the target Path 5 // (1) using the String 6 // String srcPath = "C: \ Users \ bigerf \ Desktop \ note \ 11.jpg "; 7 // String destPath = "C: \ Users \ bigerf \ Desktop \ image backup \ 11.jpg"; 8 9 // (2) use the File class 10 File srcPath = new File ("C: \ Users \ bigerf \ Desktop \ note \ 22.PNG"); 11 File destPath = new File (" C: \ Users \ bigerf \ Desktop \ image backup \ 22.PNG"); 12 13 // 2. create a channel and open the input stream in sequence , Output stream 14 FileInputStream FD = new FileInputStream (srcPath); 15 FileOutputStream fos = new FileOutputStream (destPath); 16 17 byte [] bt = new byte [1024]; 18 19 // 3. read and Write information (read and write) 20 while. read (bt )! =-1) {// read 21 fos. write (bt); // write 22} 23 24 // 4. close the stream in sequence (first turn on and then turn off) 25 fos. close (); 26. close (); 27}
(2) Use the ghost stream to copy files
1 // writable stream method, the written data will be lost 2 public static void copyFileChar () throws IOException {3 4 // obtain the target Path 5 File srcPath = new File ("C: \ Users \ bigerf \ Desktop \ note \ 33.PNG"); 6 File destPath = new File ("C: \ Users \ bigerf \ Desktop \ image backup \ 33.PNG"); 7. // create a channel to open the input stream in sequence, output stream 9 FileReader frd = new FileReader (srcPath); 10 FileWriter fwt = new FileWriter (destPath); 11 12 char [] ch = new char [1024]; 13 int length = 0; 14 // read And write information (read while writing) 15 while (length = frd. read (ch ))! =-1) {// read 16 fwt. write (ch, 0, length); // write 17 fwt. flush (); 18} 19 20 // close the stream in sequence (turn off first, then turn off first) 21 frd. close (); 22 fwt. close (); 23}
(3) using copying an image as an example to throw an exception
1 // use copying an image as an example to implement try {} cater {} finally {} using 2 public static void test () {3 // 1. obtain the target Path 4 File srcPath = new File ("C: \ Users \ bigerf \ Desktop \ note \ 55.PNG"); 5 File destPath = new File (" C: \ Users \ bigerf \ Desktop \ image backup \ 55.PNG"); 6 // 2. create a channel, first assign null value 7 FileInputStream FS = null; 8 FileOutputStream fos = null; 9 // 3. when creating a channel, you need to throw an exception 10 try {11 FS = new FileInputStream (srcPath); 12 fos = new FileOutputStream (destP Ath); 13 14 byte [] bt = new byte [1024]; 15 // 4. when reading and writing data, an exception 16 try {17 while (FS. read (bt )! =-1) {18 fos. write (bt); 19} 20} catch (Exception e) {21 System. out. println ("storage disk exception, please repair"); 22 throw new RuntimeException (e); 23} 24 25 26} catch (FileNotFoundException e) {27 System. out. println ("The resource file does not exist"); 28 throw new RuntimeException (e); 29} finally {30 31 // 5. whether there are any exceptions, You need to disable the resource (throwing exceptions separately) 32 try {33 fos. close (); 34} catch (Exception e) {35 System. out. println ("failed to close the resource file or target file! "); 36 throw new RuntimeException (e); 37} 38 39 try {40 FS. close (); 41} catch (IOException e) {42 System. out. println ("failed to close the resource file or target file! "); 43 throw new RuntimeException (e); 44} 45 46} 47}
Bytes stream = byte stream + Decoding---> Find the corresponding code table GBK Seek stream decoding: Get the default encoding method of the system to decode Compare the binary data in the image with the value in the GBK code table. During the comparison, a binary file is displayed in the code table.If no value is found, the binary data is marked as an unknown character.When I write data, it will discard unknown characters. Therefore, image copying fails (data loss) Q: When to use byte stream? When to use the livestream? Byte stream: read/write data does not need to be converted into characters that I can understand. For example, images, videos, audios... Use of the character stream: If character data is read and written. |