首页 > 代码库 > file与 byte[] 互转

file与 byte[] 互转

byte 转file

String filepath="D:\\"+getName();
          File file=new File(filepath);
          if(file.exists()){
              file.delete();
          }
          FileOutputStream fos = new FileOutputStream(file);   
          fos.write(data,0,data,.length);   
          fos.flush();   
          fos.close(); 

 

file转 byte

FileInputStream inputStream = new FileInputStream(file);
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();  
        int ch;  
        while ((ch = inputStream.read()) != -1) {  
         bytestream.write(ch);  
        }  
        byte[] data= http://www.mamicode.com/bytestream.toByteArray();
        bytestream.close();  
        inputStream.close();    
        return data;

file与 byte[] 互转