Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

Reading a csv in java is reading only nulls

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...