Question

I have five arrays of (x,y) positions for five different particles. The arrays have the form:

double [][] ProtonTracking = new double[1000][2];

(x,y) coordinates are stored in columns vertically like:

ProtonTracking = (x1,y1)
                 (x2,y2)
                 (x3,y3)
                   etc

I am trying to write these (x,y) coordinates to a .csv file in order to plot them. I have tried to use PrintWriter to do this but I'm not sure how to use it. As an extra complication this code has been created to run on multiple processors and so I am confused as to where to create the file and where to close it. Each processor generates and simulates an individual particle and then would ideally write this data to .csv. Is it possible for multiple threads to write to a .csv file at the same time? Could anyone point me in the right direction?

Was it helpful?

Solution

You have a couple of questions in your posting, so let's deal with them in order.

First question: How to write the defined array to a file using PrintWriter?

 void generateCSV(File myFile) throws IOException {
     PrintWriter pw = new PrintWriter(new FileWriter(myFile));
     for(int i= 0; i < 1000; i++) {
       pw.print(String.format("(%f, %f)%n", ProtonTracking[i][0], ProtonTracking[i][1]));
     }
  }

This will output the data for as you show above, one line per pair, surrounded by parenthesis.

Second question: Where to create the file?

That one depends on the platform. On a desktop you should be able to create the file in the application's run directory or a fixed path say in the user's home directory. Look at the java.lang.System.getProperties documentation, it will tell you how to get the location of the home directory or a temp directory which you can then use to construct a File object. On other platforms, say an Android device, you'll have to research what are available writable directories.

Two other alternatives would be to have the user provide the name of the output file as an argument or have the code just generate the output to System.out and let the user capture it where ever.

Third question: Is it possible to have multiple threads write to the same file?

Possible? yes. Do you want to do that? Probably not. The problem you would run into with multiple threads hitting the same output file is that you wouldn't know which thread wrote which data, so it would become intermingled. That would potentially result in the plot data being all confused. A writable file should be considered like an owned resource for a given thread.

Having multiple threads write to different files is perfectly fine. If each thread maintains a reference to its own ProtonTracking array and has its own output file, that should work fine. You'll want to make sure in your design all objects and data structures are thread safe or access to them is properly protected. That might mean you don't want to share a single ProtonTracking array, but instead want one for each thread.

Hope this helps getting you moving in the right direction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top