简体   繁体   English

Java FileWriter:客户端-服务器环境上的重复文件打开/关闭(多个实例)与单个打开/关闭(单个实例)

[英]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的单个实例将其关闭。

  1. What are the impacts of just using a single instance of the FileWriter? 仅使用FileWriter的单个实例有什么影响?
  2. Would there be a significant speed change when we change the Filewriter from Multiple Instance to single instance? 当我们将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.

相关问题 打开/关闭单个或多个插入调用的数据库模式 - Open/close database pattern for single vs. multiple insertion calls Selenium Java-如何登录一次并使用同一浏览器实例运行多个测试用例,而是为每个测试打开/关闭浏览器 - Selenium Java-How do I login once and run multiple test cases using the same instance of browser, instead open/close the browser for every single test 在每个查询和应用实例上休眠打开/关闭SessionFactory - Hibernate open-close SessionFactory on every query vs on app instance 使用JAVA的单实例文件存储 - Single Instance File Storage with JAVA 无论如何在Java应用程序中打开/关闭memcached服务器? - Anyway to open/close memcached server in java application? 单个 ScheduledExecutorService 实例与多个 ScheduledExecutorService 实例 - Single ScheduledExecutorService instance vs Multiple ScheduledExecutorService instances 打开和关闭Java中的mp3文件 - open and close a mp3 file in java 如何在Java中打开文件并关闭事件 - How to get file open and close an event in java 为什么需要用Java关闭文件打开的连接? - Why is it necessary to close file open connections in Java? 如何在Java中关闭自身的实例 - How to close an instance of itself in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM