1

I must read a CSV file with Java and I am doing it with Super CSV library. If I open the CSV file with a text editor (both in windows or linux), I can see a row like this:

6;;07/01/1939;58;22;47;49;69;  -   ;  -   ;  -   ;  -   ;  -   ;

The cell that seems to contain " - ", on linux is read from java as "� - � " and on windows is read as "á - á ". I need the correct representation because I have to do a comparison. Now I'm initializing a string variable = "\uFFFD - \uFFFD " (because I developed on linux), but this code do not work on windows.

Any solution different from read the file first to check the character (the current file could not have it) or initializing a string based on OS (I will implement this as a temporary solution)?

1 Answer 1

2

You should know in which encoding the CSV file is actually in.

If you open it in a text editor that displays it correctly, you should find out in that editor which encoding has been used. (eg. the encoding menu-item in notepad++ to name one).

Than you can specify it when reading it in java.

example here with utf-8 :

FileInputStream fis = new FileInputStream("test.txt"); 
InputStreamReader in = new InputStreamReader(fis, "UTF-8");

or

Scanner scanner = new Scanner(file, "UTF-8");

The problem could be that a different default character set is used on windows and linux.

(see : http://www.javapractices.com/topic/TopicAction.do?Id=42, where is stated : The FileReader and FileWriter classes are a bit tricky, since they implicitly use the system's default character encoding. )

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I used "ISO-8859-1" and worked perfectly :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.