1 package cn.zhang.io; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.io.InputStream; 11 import java.io.OutputStream; 12 13 /** 14 * 文件读取到字节数组,再从字节数组读取到文件 15 * 四个步骤 16 * 1、创建源 17 * 2、选择流 18 * 3、操作 19 * 4、释放资源 20 * @author zhang 21 * 22 */ 23 public class IOTest10 { 24 public static void main(String[] args) { 25 //指定文件源,获得该文件的字节数组 26 byte[] datas = fileToByteArray("very_nice_satellite_images_of_iran-wallpaper-2880x1620.jpg");//图片转为字节数组 27 byteArrayToFile(datas,"picture.jpg");//字节数组转为图片 28 } 29 30 /** 31 * 1、文件读取到字节数组,以程序为中心 32 * 1)、文件到程序: 33 * 源:main方法中 34 * 流:InputStream、FileInputStream 35 * 操作:输入 36 * 释放资源:需要释放 37 * 程序到字节数组: 38 * 源:内部维护,自动创建一个字节数组 39 * 流:ByteArrayOutputStream,不能使用多态,使用ByteArrayOutputStream流中的toByteArray方法,返回字节数组 40 * 操作:输出 41 * 释放资源:不需要释放 42 * 2)、结果是获得一个字节数组 43 * @param filePath 44 * @return 45 */ 46 public static byte[] fileToByteArray(String filePath) { 47 //创建源与目的地 48 File src = new File(filePath);//获得文件的源头,从哪开始传入(源) 49 byte[] dest = null;//最后在内存中形成的字节数组(目的地) 50 //选择流 51 InputStream is = null;//此流是文件到程序的输入流 52 ByteArrayOutputStream baos= null;//此流是程序到新文件的输出流 53 //操作(输入操作) 54 try { 55 is = new FileInputStream(src);//文件输入流 56 baos = new ByteArrayOutputStream();//字节输出流,不需要指定文件,内存中存在 57 byte[] flush = new byte[1024*10];//设置缓冲,这样便于传输,大大提高传输效率 58 int len = -1;//设置每次传输的个数,若没有缓冲的数据大,则返回剩下的数据,没有数据返回-1 59 while((len = is.read(flush)) != -1) { 60 baos.write(flush,0,len);//每次读取len长度数据后,将其写出 61 } 62 baos.flush();//刷新管道数据 63 dest = baos.toByteArray();//最终获得的字节数组 64 return dest;//返回baos在内存中所形成的字节数组 65 } catch (FileNotFoundException e) { 66 e.printStackTrace(); 67 } catch (IOException e) { 68 e.printStackTrace(); 69 }finally { 70 //释放资源,文件需要关闭,字节数组流无需关闭 71 if(null != is) { 72 try { 73 is.close(); 74 } catch (IOException e) { 75 e.printStackTrace(); 76 } 77 } 78 79 } 80 return null; 81 } 82 83 /** 84 * 字节数组写出到新的文件 85 *1、字节数组读入程序中 86 * 源:传入的字节数组 87 * 流:ByteInputStream 88 * 操作:读入 89 * 释放资源:不需要 90 *2、程序写出到新的文件中 91 * 源:传入的新文件目的地 92 * 流:OutputStream FileOutputStream 93 * 操作:写出 94 * 释放资源:需要 95 * @param src 96 * @param filePath 97 */ 98 public static void byteArrayToFile(byte[] src,String filePath) { 99 //创建源 100 File dest = new File(filePath);//目的地,新文件 101 //src字节数组已经存在 102 //选择流 103 InputStream is = null;//ByteArrayInputStream的父类 104 OutputStream os = null; 105 //操作 106 try { 107 is = new ByteArrayInputStream(src);//字节数组与程序之间的管道 108 os = new FileOutputStream(dest);//程序与新文件之间的管道 109 //一样的字节数组缓冲操作 110 byte[] flush = new byte[1024*10]; 111 int len = -1; 112 while((len = is.read(flush)) != -1) { 113 os.write(flush,0,len); 114 } 115 os.flush(); 116 } catch (FileNotFoundException e) { 117 e.printStackTrace(); 118 } catch (IOException e) { 119 e.printStackTrace(); 120 }finally { 121 if(null != os) {//关闭文件流 122 try { 123 os.close(); 124 } catch (IOException e) { 125 e.printStackTrace(); 126 } 127 } 128 } 129 } 130 }