JAVA流操作

java

file 转 数组,方法一:

        File file = new File("D:\\111.pdf");

// File 转数组

FileInputStream fis = new FileInputStream(file);

ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);

byte[] b = new byte[1000];

int n;

while ((n = fis.read(b)) != -1) {

bos.write(b, 0, n);

}

fis.close();

byte[] bytes = bos.toByteArray();

System.out.println(bytes);

bos.close();

file 转 数组,方法二:

        File file = new File("D:\\111.pdf");

FileInputStream fis = new FileInputStream(file);

byte[] bytes = IoUtil.readBytes(fis, true);

字节流转文件,方法一:

    /**

* 将字节流转换成文件

* @param filename

* @param data

* @throws Exception

*/

public static void saveFile(String filename,byte [] data)throws Exception{

if(data != null){

String filepath ="D:\\" + filename;

File file = new File(filepath);

FileOutputStream fos = new FileOutputStream(file);

fos.write(data,0,data.length);

fos.flush();

fos.close();

}

}

字节流转文件,方法二:

        File file = new File("D:\\111.pdf");

FileInputStream fis = new FileInputStream(file);

byte[] bytes = IoUtil.readBytes(fis, true);

FileOutputStream fos = new FileOutputStream("D://333.PDF");

IoUtil.write(fos, true, bytes);



以上是 JAVA流操作 的全部内容, 来源链接: utcz.com/z/392822.html

回到顶部