2016 - 2025

感恩一路有你

file转inputstream ByteArrayInputStream能转换成FileInputStream么?

浏览量:2897 时间:2021-03-14 02:56:02 作者:admin

ByteArrayInputStream能转换成FileInputStream么?

1、将File、FileInputStream 转换为byte数组:File file = new File("file.txt")InputStream input = new FileInputStream(file)byte[] byt = new byte[input.available()]input.read(byt)

2、将byte数组转换为InputStream:byte[] byt = new byte[1024]InputStream input = new ByteArrayInputStream(byt)

3、将byte数组转换为File:File file = new File("")OutputStream output = new FileOutputStream(file)BufferedOutputStream bufferedOutput = new BufferedOutputStream(output)bufferedOutput.write(byt)

inputstream怎么转换成字节数组?

Java中的I/O机制都是基于数据流进行输入和输出的,将流转换成字节数组保存下来是数据流传输必不可少的一部分。转换的代码如下(在具体场景下需要处理流的关闭问题)

public static byte[] toByteArray(InputStream input) throws IOException {

ByteArrayOutputStream output = new ByteArrayOutputStream()

byte[] buffer = new byte[1024*4]

int n = 0

while (-1 != (n = input.read(buffer))) {

output.write(buffer, 0, n)

}

return output.toByteArray()

}

用,请问如何将字节数组byte转换成inputStream流?

代码如下

FileInputStream instream = new FileInputStream(filename)

byte[] k=new byte[1024*1024*20]

int bloblength=instream.read(k)

byte[] blobparam=new byte[bloblength]

instream = new FileInputStream(filename)

instream.read(blobparam,0,blobparam.length)

byte[]数组如何转换成fileInputStream?

如果必须要用FileOutputStream的话那是没有办法的,因为FileOutputStream是属于比较底层的流,所有的构造方法都与文件关联。但是如果要写入blob中的话使用FileOutputStream却是有点儿多余的,因为像你那样写入文件读出来之后同样还是byte数组,所以可以直接使用OutputStream的write(byte[] b, int off, int len)方法,OFF开始标记一般设为0,len偏移量一般设为byte的length大小

file转inputstream javascriptjavascript少女 byte转成fileinputstream

版权声明:本文内容由互联网用户自发贡献,本站不承担相关法律责任.如有侵权/违法内容,本站将立刻删除。