简体   繁体   English

PrintWriter可以写入“打开”文本文件(Java)

[英]Can PrintWriter write to an “open” text file (Java)

I'm required to create a part of a java program that logs all activity "secretly" that the user is doing with the program. 我需要创建一个Java程序的一部分,该程序“秘密”记录用户正在对该程序执行的所有活动。 purely to catch people trying to "cheat" the system. 纯粹是为了抓住试图“欺骗”系统的人们。 The thing is, multiple people will be using the same program on multiple computers 问题是,多个人将在多台计算机上使用同一程序

All the information needs to be written using PrintWriter to a single text file that will, later on, be used by an administrative part of the program. 所有信息都需要使用PrintWriter写入单个文本文件,稍后该文件将由程序的管理部分使用。

 PrintWriter printer = new PrintWriter(new FileWriter(serverFolderLocation + "\\LogUserInfo.txt", true));

It's expected that 50+- computers will be using this program, every few seconds an array of 7+ lines of text is expected to be written to that file every time a specific button is pressed on a computer 预计将有50台以上的计算机使用此程序,每隔几秒钟,每按一次计算机上的特定按钮,就会将每行包含7行以上的文本写入该文件中

I know that writing text to a text file is extremely quick and this is unlikely to happen, but If 2 or more computers happens to write to the text file at the same time, while append is set to true, will data go missing? 我知道将文本写入文本文件的速度非常快,而且这种情况不太可能发生,但是如果将append设置为true时,同时有2台或更多台计算机同时写入文本文件,数据会丢失吗? or will it append normally? 还是会正常添加?

Is this even possible? 这有可能吗? 2+ devices writing data to a text file at different times? 2+台设备在不同时间将数据写入文本文件?

Do note that it is important that all the data from all 50+ computers arrive at the destinated file. 请注意,来自所有50多台计算机的所有数据都到达目标文件非常重要。

If problems are likely to occur, what other methods can be used of doing something like this, other than setting up a dedicated database? 如果可能会出现问题,除了建立专用数据库之外,还可以使用什么其他方法来执行类似的操作?

Setting append to true in the FileWriter : 在FileWriter中将append设置为true

append - boolean if true, then data will be written to the end of the file rather than the beginning. append-布尔值(如果为true),则数据将被写入文件的末尾而不是开始。

If you've got 50 machines doing this all at the same time, there's going to be conflicts. 如果您有50台计算机同时执行所有操作,那么将会有冲突。 Your computer complains when you try to modify a file that's being used by another process, don't go throwing in 50 more contenders. 当您尝试修改另一个进程正在使用的文件时,计算机会发出抱怨,不要再抛出50个竞争者。

You could try using Sockets . 您可以尝试使用Sockets

Whichever machine holds that file, designate that as the 'server' and make the other machines 'clients'. 无论哪台计算机都保存该文件,都将其指定为“服务器”,并将其他计算机设置为“客户端”。 Your clients send messages to the server, your server appends those messages to the file in a synchronised manner. 您的客户端将消息发送到服务器,服务器将这些消息以同步方式附加到文件。

You could then prevent your ~50 clients from directly changing the logs file with some network & server security. 然后,您可以通过一定的网络和服务器安全性阻止大约50个客户端直接更改日志文件。

You are simply re-inventing the wheel here, but in a very wrong way. 您只是在这里重新发明轮子,但是方式很错误 The other answer is correct: using a simple file, and having multiple distributed users write to the same file just screams for failure. 另一个答案是正确的:使用一个简单的文件,让多个分布式用户写入同一个文件,只会尖叫失败。

But I disagree with the idea to use sockets instead. 但是我不同意使用套接字的想法。 That is really low level, and requires you to implement a lot of (complicated) things yourself. 这确实很低,需要您自己实现很多(复杂的)事情。 You will have to think about network issues, multi threading, locking, buffering, ... 您将不得不考虑网络问题,多线程,锁定,缓冲等。

Sure, if this is for education, then building something like that is a challenge. 当然,如果这是为了教育,那么建立类似的东西是一个挑战。 But if you the goal here is to come to a robust solution that just works, you should rather think about using some 3rd party off-the-shelf solution. 但是,如果您的目标是要找到一个行之有效的健壮解决方案,那么您应该考虑使用一些第三方的现成解决方案。

You could start reading here . 您可以在这里开始阅读。 And then pick a framework such as logback. 然后选择一个框架,例如logback。 Alternatively, you could look into messaging services , such as ActiveMQ, or RabbitMQ. 或者,您可以研究消息传递服务 ,例如ActiveMQ或RabbitMQ。

Again: creating and collection logs in distributed environments is A) hard to get right but B) a solved problem. 再次:在分布式环境中创建和收集日志是A) 难以 解决的问题,但B)已解决的问题。

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

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