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();