File

165 阅读1分钟
InputStream与byte
InputStream转byte[]
public static byte[] inputStreamToByte(InputStream is){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024*1024];
    int n = 0;
    while (-1 != (n = is.read(buffer))) {
        baos.write(buffer, 0, n);
    }
    byte[] bytes = baos.toByteArray();
    baos.close();
    return bytes;
}
byte[]转InputStream
public static InputStream bytesToInputStream(byte[] bytes){
    InputStream is = new ByteArrayInputStream(bytes)   
    return is;
}
InputStream与File
InputStream转File
public static File inputStreamToFile(){
    InputStream is = null;
    OutputStream os = new FileOutputStream("FilePath");
    int n = 0;
    byte[] buffer = new byte[8192];
    while ((n = is.read(buffer)) != -1) {
    os.write(buffer, 0, n);
    }
    os.close();
    is.close();
}