Home > Back-end >  IO stream copy pictures
IO stream copy pictures

Time:04-21

 package test2. 

Import the Java. IO. FileInputStream;
Import the Java. IO. FileOutputStream;
import java.io.IOException;
Public class copypng {
Public static void main (String [] args) throws IOException {
FileInputStream fis=new FileInputStream (" D: \ \ itcast \ \ 1. JPG ");
FileOutputStream fos=new FileOutputStream (" C: \ \ itcast \ \ 1. JPG ");
Byte bys []=new byte [1024].
While (fis) read (bys)!=1) {
Fos. Write (bys, 0, fis. Read (bys));
}
fos.close();
Fis. Close ();
}
}


Why copy out the half byte is the original images, and can't show

CodePudding user response:

Because fis. Read (bys) read value when they didn't write it out, but to write the data of the next to get out, write file must be only half the size, instantiate the good:
Int temp=0;
While ((temp=fis. Read (bys))!=1) {
Fos. Write (bys, 0, temp);
}

CodePudding user response:

Fis. Read () method after reading a will automatically move the cursor, so judge when the read number of bytes, in the output when move down again, so always half, half of the byte at the time of judgment is skipped,
Normal cycle does not have a constant + +, use the read time need not, because he will automatically move,
  • Related