[英]Java FileWriter: Repeated File Open/Close(Multiple Instance) vs Single Open/Close(Single Instance) on Client-Server Environment
We currently have a client-server system. 我们目前有一个客户端-服务器系统。 We have this application that will monitor the performance of each transactions on our system (start-end time of a request/processing/sql queries...etc.) by writing the logs into a text file on the client's pc.
我们有这个应用程序,它将日志记录到客户端计算机上的文本文件中,从而监视系统上每个事务的性能(请求/处理/ SQL查询的开始-结束时间等)。
Our current implementation was on every request, we will open the log file, write the details of the transaction, then close the file for every request that we make. 我们当前的实现是针对每个请求,我们将打开日志文件,写入事务的详细信息,然后针对发出的每个请求关闭文件。 There is also a separate instance of the FileWriter for each client.
每个客户端还有一个单独的FileWriter实例。
We are wondering if it is better to have the text file open all throughout the use of the application and then just close it upon log-out using a single instance of the FileWriter. 我们想知道是否最好在应用程序的整个使用过程中都打开文本文件,然后在注销后使用FileWriter的单个实例将其关闭。
If you are doing a lot of writing you might see a performance improvement with a single open close of the file. 如果您正在做大量编写工作,那么单次打开文件可能会提高性能。 However, I imagine any gain would not be worth the effort as it sounds like that would be a drop in the bucket compared to other actions you are doing (sql queries especially).
但是,我想任何收获都不值得,因为与您正在执行的其他操作(尤其是sql查询)相比,这听起来像是一笔钱。
Your platform might force you to use only one FileWriter
for the same file. 您的平台可能会强制您仅对同一文件使用一个
FileWriter
。 Keep the handle open. 保持手柄打开。 You should at least use a
PrintWriter
which handles thread-synchronization internally. 您至少应使用在内部处理线程同步的
PrintWriter
。 Performance-wise, you can trade safety for performance by using (or not) FileDescriptor.sync().
在性能方面,可以通过使用(或不使用)
FileDescriptor.sync().
来为性能而FileDescriptor.sync().
安全性FileDescriptor.sync().
You can create a FileWriter
using a FileDescriptor
, which you can get from a FileOutputStream
. 您可以使用
FileDescriptor
创建FileWriter
,可以从FileOutputStream
获取它。
I second the notion to just use log4j or Java's own logging mechanism. 我仅支持使用log4j或Java自己的日志记录机制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.