简体   繁体   English

将大量字符串数据写入Java中的文件,优化选项

[英]Writing huge string data to file in java, optimization options

I have a chat like desktop java swing app, where i keep getting String type data. 我有一个像桌面Java Swing应用程序这样的聊天室,在这里我不断获取String类型数据。 Eventually the String variable keeps growing larger and larger. 最终,String变量不断增长。

1) Is it wise idea to keep the large variable in memory and only when the logging is finished save this to disk. 1)将大变量保留在内存中,并且仅在日志记录完成后才将其保存到磁盘是明智的主意。

2) If not, then should i continue saving everytime i get a new string (of length about 30-40). 2)如果没有,那么我应该每次我得到一个新的字符串(长度大约为30-40)时继续保存。

How should i go about optimizing such a desgin? 我应该如何优化这种设计?

I would use a BufferedWriter, like PrintWriter. 我会使用BufferedWriter,例如PrintWriter。 This will buffer the data for you and write every 8 KB (actually every 8192 characters). 这将为您缓冲数据并每8 KB(实际上每8192个字符)写入一次。 If you want to write more often you can use flush() or a smaller buffer. 如果您想更频繁地编写,则可以使用flush()或较小的缓冲区。

PrintWriter pw = new PrintWriter("my.log");

// will actually write to the OS, 5 times. (1000 * 40 / 8192)
for(int i = 0; i < 1000; i++) {
   pw.printf("%39d%n", i); // a 40 character number.
}

pw.flush();

or you can use 或者你可以使用

pw.println(lineOfText);

BTW: If you want to know what a really huge file looks like ;) This example writes an 8 TB file http://vanillajava.blogspot.com/2011/12/using-memory-mapped-file-for-huge.html 顺便说一句:如果您想知道真正的大文件是什么;)此示例将写入一个8 TB的文件http://vanillajava.blogspot.com/2011/12/using-memory-mapped-file-for-huge.html

Perhaps you should use a StringBuilder . 也许您应该使用StringBuilder Append each new message to it, and at the end convert it to a string. 将每条新消息附加到该消息,最后将其转换为字符串。

For example, 例如,

StringBuilder sb = new StringBuilder();  
// Do your code that continuously adds new messages/strings.
sb.append(new_string);  
// Then once you are done...
String result = sb.toString(); 

If you were to have some string, say String message , and every time you got a new message/string you did message += new_string , it will eat up more memory. 如果您有一些字符串,请说String message ,并且每次收到新的消息/字符串时,都会执行message += new_string ,它将占用更多的内存。

As suggested by Viruzzo, only save so much, then discard the earlier strings at some point. 正如Viruzzo所建议的那样,只保存那么多,然后在某个时候丢弃早期的字符串。 Don't hold on to every message forever. 不要永远保留每条消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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