my Java program doesn't write anything in my .csv file, even though there is no "file not found" error

huangapple go评论59阅读模式
英文:

my Java program doesn't write anything in my .csv file, even though there is no "file not found" error

问题

我的代码是:

FileWriter writer = new FileWriter("fisiere/audit.csv", true);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime now = LocalDateTime.now();
writer.append(dtf.format(now));
writer.append(",");
writer.append("insereazaTabelCandidatiLinie");
writer.append(",");
writer.append(Thread.currentThread().getName());
writer.append("\n");

我想打开 .csv 文件并在其中写入调用的方法名称、调用的时间和日期。这段代码在主程序中运行,可以将我需要的内容写入文件,但是当我在我的方法中使用相同的代码时,它却不会写入任何内容。我已经调用了这个方法,在执行上述代码行之前,我通过将一条简短的消息打印到屏幕上进行了检查,这条消息已经显示在屏幕上。

英文:

My code is :

	FileWriter writer = new FileWriter("fisiere/audit.csv", true);
	DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
	LocalDateTime now = LocalDateTime.now();
	writer.append(dtf.format(now));
	writer.append(",");
	writer.append("insereazaTabelCandidatiLinie");
	writer.append(",");
	writer.append(Thread.currentThread().getName());
	writer.append("\n");

I want to open the .csv file and write in it the name of the called method and time and date of calling, this code works in main and it writes what I need in the file, but when I use the same code in my method it doesn't write anything. The method is called, I checked by printing to the screen a short message right before executing the lines above, the message has been displayed to the screen.

答案1

得分: 1

数据被追加到FileWriter后,不一定会立即写入磁盘。其中一个原因是写入磁盘可能是一个相对较慢/昂贵的操作。为了解决这个问题,传入的数据可能会被写入内存中的缓冲区,稍后通过调用"flush"或"close"来手动将数据刷新到磁盘,或者在内存缓冲区变满时刷新。确保数据在程序结束时刷新的最简单方法是使用"try-with-resources"块来控制FileWriter的生命周期。

这将自动调用"close",从而在try块完成时导致数据被刷新。

编辑 - 何时进行刷新

何时刷新数据将在很大程度上取决于上下文。一般来说,最大的写入频率应该是每当完整的消息已被写入缓冲区时。例如,在上面的代码中,不要在新行之前调用flush,因为这可能会导致文件读取器看到不完整的数据(线程名称可能会丢失)。

根据预期的写入吞吐量,最大频率可能不如期望地有效利用系统资源。在这种情况下,可以在刷新之前在内存中批量处理消息,或者在刷新之间等待一些时间间隔,这可能更可取。

注意在文件更准确地反映进程的实时状态与进程执行更多工作之间的权衡。

英文:

Data may not be written to disk as soon as it is appended to the FileWriter. One reason for this is writing to disk can be a relatively slow/expensive operation. To address this, incoming data may be written to a buffer in memory instead and 'flushed' to disk at a later time manually by calling 'flush' or 'close' or if the memory buffer becomes full. The easiest way to ensure data is flushed at the end of the program is by scoping the lifespan of the FileWriter using a 'try-with-resources' block.

public static void main(String[] args) throws Exception
	{
		LocalDateTime now = LocalDateTime.now();
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
		try(FileWriter writer = new FileWriter("fisiere/audit.csv", true))
		{
			writer.append(dtf.format(now));
			writer.append(",");
			writer.append("insereazaTabelCandidatiLinie");
			writer.append(",");
			writer.append(Thread.currentThread().getName());
			writer.append("\n");
		}
	}

This will automatically call 'close' which will cause data to be flushed when the try block completes.

Edit - High-level when to flush

When to flush data will be largely context specific. In general, the maximum write frequency should be every time a complete message has been written to the buffer. Ex: in the above, don't call flush until new line because it may lead to a file reader seeing incomplete data (the thread name may be missing).

Depending on the expected write throughput, the maximum frequency may use system resources less efficiently than desired. In this case, batching messages in memory before flushing or waiting some time interval between flushing may be preferred.

Note the trade-off between the file more accurately reflecting the real-time state of the process vs more work being done by the process.

huangapple
  • 本文由 发表于 2020年9月19日 20:58:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63969066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定