2

The goal is to periodically read files from a folder to which another program outputs them, and then feed the files into another part of my code.

How can I accomplish this with the best trade off between performance and readable and easy code?

(I need to accomplish this in both C# and Java. hence the double tagging, (and no, this is not homework :))

akapulko2020
  • 1,079
  • 2
  • 22
  • 36
  • 2
    Try the `FileSystemWatcher` in c# – Oskar Kjellin Feb 06 '12 at 13:54
  • The efficiency is dependent on the file size and how often you poll them. But you'll avoid lots of pain if writer writes files with temporary name like 'file.dat.partial' and when its done, renames it to 'file.dat'. Rename is atomic in all operating systems. Your poll should only look these non-partial files and delete them after successful fetch. – Teemu Ikonen Feb 06 '12 at 13:56
  • 1
    For FileSystemWatcher, see http://stackoverflow.com/questions/239988/filesystemwatcher-vs-polling-to-watch-for-changes and http://stackoverflow.com/questions/960318/filesystemwatcher-fails-to-access-network-drive – Kramii Feb 06 '12 at 13:57

2 Answers2

1

In Java you can use a watch service. I believe it uses the same underlying system calls as C# would.

http://docs.oracle.com/javase/tutorial/essential/io/notification.html

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

I/O is a bottle neck for most programs but if you're going for performance there are a couple things you can do to help. One, is only read when you need to. This can be accomplished by using FileSystemWatcher to tell you when the file is changed. The second is, if possible, spawn a new thread to do the I/O if you can continue without the operation being completed.

OnResolve
  • 4,016
  • 3
  • 28
  • 50