java如何将一个InputStream写入文件?

如题所述

java通过InputStream读取和写入文件操作实例代码

1. File to InputStream

File file = new File("file.xml");
InputStream inputStream = new FileInputStream(file);

2.InputStream to File

InputStream inputStream = new FileInputStream("file.xml");

OutputStream  outputStream = new FileOutputStream("file-new.xml");

int bytesWritten = 0;
int byteCount = 0;

byte[] bytes = new byte[1024];

while ((byteCount = inputStream.read(bytes)) != -1)
{
outputStream.write(bytes, bytesWritten, byteCount);
bytesWritten += byteCount;
}
inputStream.close();
outputStream.close();

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答