將Java InputStream的內容寫入OutputStream的簡單方法今天我驚訝地發(fā)現,我無法找到任何簡單的方法來編寫InputStream轉到OutputStream在爪哇。顯然,字節(jié)緩沖區(qū)代碼編寫起來并不困難,但我想我只是缺少了一些可以讓我的生活變得更簡單(而且代碼更清晰)的東西。所以,考慮到InputStream in和一個OutputStream out,是否有更簡單的方法來編寫以下內容?byte[] buffer = new byte[1024];int len = in.read(buffer);while (len != -1) {
out.write(buffer, 0, len);
len = in.read(buffer);}
3 回答

陪伴而非守候
TA貢獻1757條經驗 獲得超8個贊
/* You can get Path from file also: file.toPath() */Files.copy(InputStream in, Path target)Files.copy(Path source, OutputStream out)
file.toPath()
File.createTempFile()
REPLACE_EXISTING
FileAlreadyExistsException
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING)
添加回答
舉報
0/150
提交
取消