I. Copying a picture using a byte stream
The byte stream method public static void CopyFile () throws IOException { //1. Gets the target path //(1) can be passed by string / /String Srcpath = "E://11.jpg"; String destpath = "e://22.jpg"; (2) file type Srcpath = new ("E://11.jpg"); File DestPath = new file ("E://22.jpg"); 2. Create channel, turn on input stream, output stream fileinputstream fis = new FileInputStream (srcpath); FileOutputStream fos = new FileOutputStream (destpath); byte[] bt = new byte[1024]; 3. Read and write information (side read side write) while (Fis.read (BT)! =-1) {//Read fos.write (BT);//write } //4. Turn off the stream (first on, back off, open first off) fos.close (); Fis.close (); }
Two. Copying a picture using a character stream
Character stream method, the data written will have lost public static void Copyfilechar () throws IOException { //Get the target path file Srcpath = new file ("E ://11.jpg ");
File DestPath = new file ("E://22.jpg"); Create the channel, open the input stream in turn, output stream filereader frd = new FileReader (srcpath); FileWriter FWT = new FileWriter (destpath); char[] ch = new char[1024]; int length = 0; Read and write information (edge read side write) while (length = frd.read (ch)) =-1) {//Read fwt.write (ch,0,length);// write Fwt.flush () ; } Close the flow in turn (first open, after close, after open first off) frd.close (); Fwt.close (); }
Three. Exception handling in the process of copying pictures
For example, to copy a picture, implement try{}cater{}finally{} using public static void Test () {//1. Get target path File SRCPA th = new File ("e://11.jpg");
File DestPath = new file ("E://11.jpg");
2. Create channel, first assign null value FileInputStream FIS = null; FileOutputStream fos = null; 3. You need to throw an exception when creating a channel try {fis = new FileInputStream (Srcpath); FOS = new FileOutputStream (destpath); byte[] bt = new byte[1024]; 4. You need to throw an exception when reading and writing data try {while (Fis.read (BT)! =-1) {Fos.write ( BT); }} catch (Exception e) {System.out.println ("Storage disk abnormal, please repair"); throw new RuntimeException (e); }} catch (FileNotFoundException e) {System.out.println ("resource file does not exist"); throw new RuntimeException (e); }finally{
5. If there is no exception, you need to close the resource (throw exception separately) try {fos.close (); } catch (Exception e) {System.out.println ("resource file or target file shutdown failed! "); throw new RuntimeException (e); }
try {fis.close (); } catch (IOException e) {System.out.println ("resource file or target file shutdown failed! "); throw new RuntimeException (e); } } }
|
Word stream = byte stream + decode ---> find the corresponding Code table GBK
character Stream decoding : get the system default encoding method to decode
The binary data in the picture and the values in the GBK code table are compared, the binary file will appear in the Code table to find no corresponding value, he will mark the binary data as unknown characters , when I write the data is the unknown characters will be discarded. So it will cause the image copy to be unsuccessful (lost data)
Question: When do I use a byte stream? When do I use character streams?
scenario using a byte stream: read-write data does not need to be converted to characters that I can understand. For example: pictures, video, audio ...
A scene using a character stream : If you read and write character data.
|
Java IO stream Copy Picture