file轉(zhuǎn)inputstream ByteArrayInputStream能轉(zhuǎn)換成FileInputStream么?
ByteArrayInputStream能轉(zhuǎn)換成FileInputStream么?1、將File、FileInputStream 轉(zhuǎn)換為byte數(shù)組:File file = new File("fil
ByteArrayInputStream能轉(zhuǎn)換成FileInputStream么?
1、將File、FileInputStream 轉(zhuǎn)換為byte數(shù)組:File file = new File("file.txt")InputStream input = new FileInputStream(file)byte[] byt = new byte[input.available()]input.read(byt)
2、將byte數(shù)組轉(zhuǎn)換為InputStream:byte[] byt = new byte[1024]InputStream input = new ByteArrayInputStream(byt)
3、將byte數(shù)組轉(zhuǎn)換為File:File file = new File("")OutputStream output = new FileOutputStream(file)BufferedOutputStream bufferedOutput = new BufferedOutputStream(output)bufferedOutput.write(byt)
inputstream怎么轉(zhuǎn)換成字節(jié)數(shù)組?
Java中的I/O機(jī)制都是基于數(shù)據(jù)流進(jìn)行輸入和輸出的,將流轉(zhuǎn)換成字節(jié)數(shù)組保存下來是數(shù)據(jù)流傳輸必不可少的一部分。轉(zhuǎn)換的代碼如下(在具體場(chǎng)景下需要處理流的關(guān)閉問題)
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()
}
用,請(qǐng)問如何將字節(jié)數(shù)組byte轉(zhuǎn)換成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[]數(shù)組如何轉(zhuǎn)換成fileInputStream?
如果必須要用FileOutputStream的話那是沒有辦法的,因?yàn)镕ileOutputStream是屬于比較底層的流,所有的構(gòu)造方法都與文件關(guān)聯(lián)。但是如果要寫入blob中的話使用FileOutputStream卻是有點(diǎn)兒多余的,因?yàn)橄衲隳菢訉懭胛募x出來之后同樣還是byte數(shù)組,所以可以直接使用OutputStream的write(byte[] b, int off, int len)方法,OFF開始標(biāo)記一般設(shè)為0,len偏移量一般設(shè)為byte的length大小