简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM