allenfromspace
allenfromspace

Reputation: 140

Java: Problems reading/writing UTF-8 .csv file saved in Excel

I'm working on a Java application that accepts .csv files from the user, then reads and writes them to a temp folder on the server to do some processing. Here's some of my IO code:

            br = new BufferedReader(new InputStreamReader(in));
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));

            String str;
            while ((str = br.readLine()) != null) {
                out.write(str);
                out.newLine();
            }
            out.flush();

When debugging here and looking at the output file in Excel, it looks exactly as I expect. Let's say that the user inputs a csv file where the first cell's value is "Foo" (no special characters); that all seems to appear in Excel and the debugger. But later, I'll parse the output file for that value, just using value.contains("Foo"). This returns false only when the csv file is saved in csv UTF-8 format in Excel, even though according to Excel and the debugger value has a value of "Foo".

Why would this happen?

Upvotes: 0

Views: 712

Answers (1)

SebastianX
SebastianX

Reputation: 209

problem could be when you later read the file, you have to read it in "UTF-8" encoding like

       InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);

Upvotes: 3

Related Questions