简体   繁体   中英

How write huge string to file using java

So basically I have:

FileOutputStream fos = new FileOutputStream(dir + "/" + name);
fos.write(bImg);
fos.close();

Now here sometimes bImg could contains more than 5MB which I happen to get performance problem. So I might need to write part to part of the string to the file. Something like writing 5000 chars to the file first then got to next 5000 chars and so on. But I don't really know how to do that.

Tried:

String file = dir + "/" + name;
FileOutputStream fos = new FileOutputStream(new File(file));
BufferedOutputStream buffOut=new BufferedOutputStream(fos);

buffOut.write(bImg); 

buffOut.flush();
buffOut.close();

But no luck still having same problem

You need to wrap the output string in a BufferedOutputStream. It will handle writing the data in chunks for you. Remember to flush the BufferedOutputStream when you're done so that all data ends up in your file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM