Java中将一张图片从一个地方拷贝到另一个地方
将一张图片从一个地方拷贝到另一个地方
1 package lw.InputStream; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 9 /* 10 11 练习: 将一张图片拷贝到另外一个地方。 12 13 */ 14 public class Demo3 { 15 16 /** 17 * @param args 18 * @throws IOException 19 */ 20 public static void main(String[] args) throws IOException { 21 // TODO Auto-generated method stub 22 23 copeImage(); 24 } 25 26 public static void copeImage() throws IOException{ 27 //一遍读取一遍写入。 28 //无论你是是使用的输出流还是输入流,操作的一定是文件。 29 30 //1.读取数据 //输入字节流 31 //1.找到需要复制的图片 32 File file = new File("D:\\images\\01.jpg"); // NotFoundException 33 FileInputStream inputStream = new FileInputStream(file); 34 File file1 = new File("D:\\01.jpg"); 35 FileOutputStream outputStream = new FileOutputStream(file1); 36 byte[] b = new byte[1024]; 37 //int count = 0; 38 while(inputStream.read(b)!=-1){ 39 //2.写入数据 //输出字节流 40 outputStream.write(b); 41 } 42 43 //关闭流 关闭流的原则: 先开后关 后开的先关。 44 outputStream.close(); 45 inputStream.close(); 46 47 48 49 50 51 } 52 53 }

浙公网安备 33010602011771号