[英]Export 2-D object array (Object[][] array) into a .csv file in Java
I'm working on a java project and using Eclipse with JDK 1.7. 我正在研究一个Java项目,并将Eclipse与JDK 1.7一起使用。 I have 2-D array of object type as
Object[][] array
. 我有2D对象类型的数组作为
Object[][] array
。 I wish to store the contents (export) of array to a csv file (each value in a separate column for each row) on the disk. 我希望将数组的内容(导出)存储到磁盘上的一个csv文件(每个值在每一行的单独列中)。 Here is the code I'm trying:
这是我正在尝试的代码:
FileOutputStream fos = new FileOutputStream("newFile.csv");
ObjectOutputStream oos = new ObjectOutputStream(fos);
for(int i=0;i<array.length;i++)
{
for(int j=0;j<array[0].length;j++)
{
oos.writeObject(array);
oos.writeObject(",");
}
}
oos.close();
The output stored in file newFile.csv
is as follows: 存储在文件
newFile.csv
的输出如下:
¬íur[[Ljava.lang.Object;¿ûSäkÛÊxpur[Ljava.lang.Object;ÎXŸs)lxpt1t1tClosedt100.00000000t
0.00000000uq~t2t1tClosedt200.00000000t100.00000000uq~t3t1tClosedt100.00000000t50.00000000t q~q~q~q~q~q~q~q~q~q~q~q~q~q~q~q~q~q~q~q~q~q~q~q~q~q~q~q~
which is some garbage value. 这是一些垃圾值。 I need help to figure out what is wrong in this program (how to convert object into a string before storing, this is what I'm guessing the error is), and how to resolve the issue.
我需要帮助找出此程序中的问题(如何在存储之前将对象转换为字符串,这是我猜测的错误所在),以及如何解决该问题。
I found a help on http://www.tutorialspoint.com/java/io/objectinputstream_readobject.htm but it deals with byte[]
and String
, not with 2-D object array. 我在http://www.tutorialspoint.com/java/io/objectinputstream_readobject.htm上找到了帮助,但它处理的是
byte[]
和String
,而不是二维的对象数组。 Similarly, http://www.mkyong.com/java/how-to-write-to-file-in-java-fileoutputstream-example/ deals with byte[]
only. 同样, http: //www.mkyong.com/java/how-to-write-to-file-in-java-fileoutputstream-example/只处理
byte[]
。 I have also found a solution at Java - How to write ArrayList Objects into txt file? 我还在Java中找到了一种解决方案-如何将ArrayList对象写入txt文件? , where
ArrayList<String> MenuArray = new ArrayList<String>();
,其中
ArrayList<String> MenuArray = new ArrayList<String>();
is used instead of 2-D object array, and also the solution is not complete. 使用“二维对象”代替“二维对象数组”,解决方案也不完整。 I also find a help at Using FileUtils in eclipse I'm getting the similar error.
我也在Eclipse中使用FileUtils时找到了帮助,我遇到了类似的错误。 The solution says use:
解决方案说使用:
FileUtils.writeLines(new File("input.txt"), array);
However, I receive an error that method writeLine(File, Collection<>) is not applicable for WriteLine(File, Object[][])
. 但是,我收到一个错误,指出
method writeLine(File, Collection<>) is not applicable for WriteLine(File, Object[][])
。 Further a solution in ADO.NET exists for reading csv file and store data in 2-D array at Reading a csv file and storing the data in a 2-D object array , but I need the other way and also solution in not for Java. 进一步,ADO.NET中存在一种解决方案,用于在读取csv文件并将数据存储在2-D对象数组中时读取csv文件并将数据存储在2-D数组中 ,但是我需要其他方法,并且也需要解决方案,而不是Java 。
Both the methods I used are not working. 我使用的两种方法均无效。 Please note my 2-D array is of
Object
type not String
. 请注意,我的二维数组的
Object
类型不是String
。 Any help is appreciated. 任何帮助表示赞赏。
FileOutputStream fos = new FileOutputStream("newFile.csv");
ObjectOutputStream oos = new ObjectOutputStream(fos);
for(int i=0;i<array.length;i++)
{
for(int j=0;j<array[i].length;j++)
{
oos.writeObject(array[i][j]);
if ( j < array[i].length -1 )
{
oos.writeObject(",");
}
else
{
oos.wrinteObject("\n");
}
}
}
oos.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.