在java中读取csv只读取null [英] Reading a csv in java is reading only nulls

查看:1690
本文介绍了在java中读取csv只读取null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在java中使用opencsv读取csv文件。该文件由SAS代码生成。当我尝试读取文件,它给我所有的null值,即使有文件中的值。但是,如果我打开文件并使用保存为选项重新保存文件是正确读取。因为我需要自动完成这个过程而不需要任何手动干预,所以可以是什么解决方案。

I am trying to read a csv file using opencsv in java. The file is generated by a SAS code. When I try reading the file its giving me all null values even though there are values in the file. However if I open the file and re-save it using save as option the file is read correctly. What can be the solution since I need to automate the process completely without any manual intervention.

推荐答案

很难说,什么问题是,但我怀疑问题是文件编码或可能有一个字节顺序标记(BOM)在开始,表明它是unicode。你的库将无法解析它,这将解释你的空值。重新保存它可能会删除BOM或将其保存在另一个字符编码下。

It's hard to say without knowing precisely what the problem is, but I suspect the problem is the file encoding or perhaps there is a byte order mark (BOM) at the beginning indicating that it is unicode. Your library wouldn't be able to parse it and that would explain your null values. Resaving it probably removes the BOM or saves it under another character encoding.

使用十六进制编辑器打开文件,并逐个字节检查文件在保存前后。如果您没有看到相同的值,那么这是您的原因。

Open the file with a hex editor and check byte by byte what the file contains before and after the save. If you don't see the same values, then that is your reason.

在这种情况下,解决方案将传递正确的输入流,如FileReader该库具有正确的字符编码。我不太熟悉SAS,但一个替代方法可能是直接更改文件导出到更合理的东西的编码。 详情请参阅此处

In that case, the solution would be to pass the proper input stream such as a FileReader to the library with the proper character encoding. I'm not too familiar with SAS but an alternative might be to change the encoding directly of the file being exported to something more reasonable. See here for details.

这篇关于在java中读取csv只读取null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆