java 如何将文件保存在内存中并读取文件输出流?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37385908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save file in memory and read file output stream?
提问by DarioBB
When I save file I use:
当我保存文件时,我使用:
File file = new File(filename);
But, since I no longer have privileges to write to folders,I would like rather to save it to memory and then read file to FileOutputStream.
但是,由于我不再具有写入文件夹的权限,我宁愿将其保存到内存中,然后将文件读取到 FileOutputStream。
I have read I can save file to memory with this approach:
我读过我可以用这种方法将文件保存到内存中:
new ByteArrayOutputStream();
How would whole code look like? I can't figure it out how to write it properly after upload is done.
整个代码会是什么样子?上传完成后我不知道如何正确编写它。
Edit: I'm using Vaadins upload plugin:
编辑:我正在使用 Vaadins 上传插件:
public File file;
public OutputStream receiveUpload(String filename,
String mimeType) {
// Create upload stream
FileOutputStream fos = null; // Stream to write to
file = null;
if(StringUtils.containsIgnoreCase(filename, ".csv")){
try {
file = new File(filename);
fos = new FileOutputStream(file);
} catch (final java.io.FileNotFoundException e) {
new Notification("Error", e.getMessage(), Notification.Type.WARNING_MESSAGE)
.show(Page.getCurrent());
return null;
}
} else {
new Notification("Document is not .csv file", Notification.Type.WARNING_MESSAGE)
.show(Page.getCurrent());
return null;
}
return fos; // Return the output stream to write to
}
回答by David SN
public OutputStream receiveUpload(String filename,
String mimeType) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
return byteArrayOutputStream;
}
You can get the content written in the stream using:
您可以使用以下方法获取写入流中的内容:
byte[] dataWrittenInTheOutputStream = byteArrayOutputStream.toByteArray();
Or you can write the contents to another OutputStream:
或者您可以将内容写入另一个 OutputStream:
byteArrayOutputStream.writeTo(System.out);
Or:
或者:
file = new File(filename);
fos = new FileOutputStream(file);
byteArrayOutputStream.writeTo(fos);
回答by Manushin Igor
- Please avoid using ByteArrayOutputStream/ByteArrayInputStream, because they are not chunked and creates huge arrays internally
- Please avoid using methods
.toByteArray()
, because they will require additional array allocation - Large allocation is not so optimal than several smaller objects allocations, because the oldest generation will be used. See details in this answer.
- 请避免使用 ByteArrayOutputStream/ByteArrayInputStream,因为它们没有分块并在内部创建巨大的数组
- 请避免使用方法
.toByteArray()
,因为它们需要额外的数组分配 - 大分配没有几个较小的对象分配那么优化,因为将使用最旧的一代。请参阅此答案中的详细信息。
What is my recommendation:
我的建议是什么:
- Use
Buffer
class from okiolibrary. It is chunked in memory and support InputStream/OutputStream bindings - Use async way of data read to increase performance. However, I will show synchronous way.
- 使用okio库中的
Buffer
类。它在内存中分块并支持 InputStream/OutputStream 绑定 - 使用异步数据读取方式来提高性能。但是,我将展示同步方式。
So, short code will be:
所以,短代码将是:
Buffer buffer = new Buffer();
buffer.readFrom(previouslyCreatedFileStream); // of course, it is better to use
buffer.writeTo(someOutputStream);