0

I've wrote a little program for my work (IT-Support at university) to record all all inquiries received and evaluate them later. Because more than one receives inquiries at the same time the program must run on more than on pc simultaneously. At the moment we are not able to have our own server (e.g. MySQL) so my solution was to write a program that's saving the inquiries to a single file on a fileserver. The file is opened with myStatFile = new File(myFilepath + "/data/statistic.dat"); and wrote to with in = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(myFile, true), "UTF-8"));. It works pretty well but as the statistic.dat file becomes larger and larger the program needs more time to open the file, write to it and close it.

So my question to the more experienced programmers here is: Is there a more efficient way to do that or is the only possibility to improve the performance to get a server with a database like MySQL? Does anyone see another way without using a server but a simple fileserver?

Edit: First of all I thank everyone who answered my question. I know it's a very broad question with a lot of solutions but after reading your answers I've got an idea of what is possible and what's the way to go now. Thank you!

cmtjk
  • 359
  • 2
  • 12
  • Only way I see is a database (preferring postgresql) – Verim Feb 12 '15 at 12:41
  • If you have a file server I recommend you to use SQLIte which just need a file to run. You can create the database file on this fileserver then access it from the applications. – Jorge Campos Feb 12 '15 at 12:41
  • While this is a reasonably well-written question I'm going to vote to close as too broad as there are a lot of solutions and strategies to deal with a problem like this. You could use a database of some kind, or a cloud solution, or write a server app that can store requests or.... – jpw Feb 12 '15 at 12:42
  • [`java.nio.channels.FileChannel`](http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html) provides efficient means for I/O. Use it instead of `File`. – mike Feb 12 '15 at 12:56
  • You could consider creating a file named with today's date so that you don't need to manage this one HUGE file as time passes: `myStatFile = new File(myFilepath + String.format("/data/statistic_%s.dat", new Object[] {new SimpleDateFormat("yyyy-MM-dd").format(new Date())}));` – james Feb 12 '15 at 13:35

2 Answers2

2

You have set the second parameter to true in your FileOutputStream constructor. That instructs the stream to append new data to the file.

This does not slow down as the file gets larger. Both the Windows and Linux / BSD / Unix heritage operating systems handle file-appending very efficiently indeed. You should open, write, and close as quickly as you can. Don't hold the file open longer than you must.

You can arrange for your Java program to lock the file while you're writing it. That will prevent multiple users (you and your co-workers) from potentially trying to append simultaneously. See here. How can I lock a file using java (if possible)

You still should consider a database for this application, but not because appending records to files is slow.

By the way, there are several good and free (free as in speech, free as in kittens) support desk ticketing web applications available.

Community
  • 1
  • 1
O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • Thank you for your helpful comment. So the best solution seems to be a database but at the moment this has to do it. Thanks for the hint with locking the file but as you mentioned Windows/Mac OS seem to have a very good file-appending handling. The second thing you is to close the file as soon as possible. To clarify: Creating the object opens the file? So the best code should be `in = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(myFile, true), "UTF-8")); in.write("hello"); in.close()`? – cmtjk Feb 12 '15 at 17:12
0

I would certainly insist on using a database server. Your application isn't very scalable, otherwise.

To Ollie's point, there are a few free options for support desk web apps out there. I've used osticket and it's amazing how versatile it is.

Erutan409
  • 730
  • 10
  • 21