简体   繁体   English

将数组写入 csv java,应用程序在多个处理器上运行

[英]Writing array to csv java, with app running on multiple processors

I have five arrays of (x,y) positions for five different particles.我有五个不同粒子的 (x,y) 位置数组。 The arrays have the form:数组具有以下形式:

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

(x,y) coordinates are stored in columns vertically like: (x,y) 坐标垂直存储在列中,例如:

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.我正在尝试将这些 (x,y) 坐标写入 .csv 文件以绘制它们。 I have tried to use PrintWriter to do this but I'm not sure how to use it.我曾尝试使用 PrintWriter 来执行此操作,但我不确定如何使用它。 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.每个处理器生成并模拟一个单独的粒子,然后理想地将此数据写入 .csv。 Is it possible for multiple threads to write to a .csv file at the same time?多个线程是否可以同时写入 .csv 文件? Could anyone point me in the right direction?有人能指出我正确的方向吗?

You can use a library called as OpenCsv which is freely available. 您可以使用免费提供的名为OpenCsv的库。

This will help you to read and write .csv files very easily. 这将帮助您非常轻松地读取和写入.csv文件。

Refer this link http://viralpatel.net/blogs/java-read-write-csv-file/ 请参考此链接http://viralpatel.net/blogs/java-read-write-csv-file/

Its very easy. 这很容易。 Hope You will get this. 希望你能得到这个。

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? 第一个问题: 如何使用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. 查看java.lang.System.getProperties文档,它将告诉您如何获取主目录或临时目录的位置,然后可以使用它们来构造File对象。 On other platforms, say an Android device, you'll have to research what are available writable directories. 在其他平台(例如Android设备)上,您必须研究可用的可写目录。

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. 另两种选择是让用户提供输出文件的名称作为参数,或者让代码仅将输出生成到System.out,然后让用户在任何地方捕获它。

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. 如果每个线程都维护对自己的ProtonTracking数组的引用并具有自己的输出文件,则应该可以正常工作。 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. 这可能意味着您不想共享一个ProtonTracking数组,而是希望为每个线程共享一个。

Hope this helps getting you moving in the right direction. 希望这有助于您朝正确的方向前进。

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

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