1

I want to write to a csv file in UTF-8 in java

I am using BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("temp.csv"),Charset.forName("UTF-8").newEncoder())); after searching the internet

I am still getting illegal characters.

I want to write "Kürzlich" to my file and later on read and write again from the same file. When I do so I get "Kürzlich"

How I am getting "Kürzlich": I am parsing a xml file using DOM.

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    InputStream openstream = url.openStream();
    Document doc = dBuilder.parse(openstream);
    doc.getDocumentElement().normalize();

and then I extract my string.

I do not care how it is shown on the screen. I want to compare the stuff I write in the file with another file which is converted perfectly.

Is it happening because of DOM structure? Is there a way around?

1 Answer 1

1

You appear to be writing UTF-8, but I don't see how you are reading UTF-8. Most likely you are reading the default encoding.

Try wrapping the openstream with an InputStreamReader specifying the encoding you want.

I suggest you try this to show you can write and read UTF-8

String text = "Kürzlich";
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("test.txt"), "UTF-8")));
pw.println(text);
pw.close();

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt"), "UTF-8"));
String line = br.readLine();
br.close();
System.out.println("Text is the same is " + (line.equals(text)));

prints

Text is the same is true
Sign up to request clarification or add additional context in comments.

7 Comments

I am reading the same way BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(yourFile),Charset.forName("UTF-8").newDecoder())); But it comes later on so should not have anyeffect. It does not even write properly
Are you sure you are reading UTF-8 correctly when you say it is not correct? How about using the in to parse from instead of creating it when it's too late?
Assuming I do not read. That is step 2. Just writing to a file using the code mentioned I am not getting correct result.
How do you know it is not the correct result if you are not reading it?
I am doing an equals from the from source A and and this file.I tired it before writing to the file too. I check what the file contains in VI (as it does not contain any system encoding) and it shows me illegal characters
|

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.