try(Resource res = xxx;Resource res = xxx;)//可指定多个资源{.....}
代码展示
privatestaticbyte[]fileToByte(String src){//创建源
File file =newFile(src);// ByteArrayOutputStream bis = null;
InputStream is = null;//建立流(选择流)try(InputStream is =newFileInputStream(file);
ByteArrayOutputStream bis =newByteArrayOutputStream();){// is = new FileInputStream(file);// bis = new ByteArrayOutputStream();byte[] b =newbyte[1024*4];//建立缓存容器4Kint len =-1;//读入的长度//操作while((len=is.read(b))!=-1){
bis.write(b,0, len);//读入到内存}return bis.toByteArray();}catch(FileNotFoundException e){
e.printStackTrace();}catch(IOException e){
e.printStackTrace();}return null;}privatestaticvoidbyteToFile(byte[] data,String destination){//创建源
File file =newFile(destination);//选着流// OutputStream os = null;// InputStream bos = null;try(OutputStream os=newFileOutputStream(file);
InputStream bos =newByteArrayInputStream(data);){// os = new FileOutputStream(file);// bos = new ByteArrayInputStream(data);//操作byte[] b =newbyte[1024*4];int len =-1;while((len = bos.read(b))!=-1){
os.write(b,0, len);}
os.flush();//这个东西就像我们的那个拆分这个流的东西,//如果不写的也不会出现问题,关闭文件的时候也会给他清除一下缓存}catch(FileNotFoundException e){
e.printStackTrace();}catch(IOException e){
e.printStackTrace();}}