Problem generating .txt files with IntelliJ IDEA

Answered

Hi!

I have an issue generating .txt files with IntelliJ IDEA. I use the simplest version of creating .txt files i.e. with BufferedWriter/BufferedReader like this:

- for writing:

try {
    BufferedWriter bw = new BufferedWriter(new FileWriter("points.txt"));
    bw.write(points);
    bw.close();
}catch (IOException e){
    e.printStackTrace();
}

- for reading:

File file = new File("points.txt");
if(file.exists()) {
    try {
        BufferedReader br = new BufferedReader(new FileReader(String.valueOf(file.toPath())));
        System.out.println(br.readLine());
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

But when I open the .txt file both in IntelliJ IDEA or in Notepad, the content is encoded and unreadable. 

How can I generate readable plain .txt files with IntelliJ IDEA?

 

Many thanks!

Sammy

 

0
3 comments

Hi Samy Sylvester 

It could be related to the data you're writing. Please verify:

  • If “points” is an int or something not explicitly converted to String then you'd end with a garbled file. So please convert the data you want to write into “points” to String.
  • Specify the charset UTF-8 to avoid situations where the file looks garbled in other tools or machines. You can set it explicitly or through the Project Encoding via:
    *   File | Settings | Editor | File Encodings (Windows/Linux)
    *   IntelliJ IDEA | Settings | Editor | File Encodings (macOS)
  • You can ensure the file shows as UTF-8 in the status bar:
1

Hi Monica!

Thank you very much for your kind answer! I'll keep in mind all the information you offered me here! Also, your answer helped me solving my issue on this subject, since my “points" variable was an integer and converting it to String made the content from the .txt file completely readable! :)

 

Wishing you all the best, I thank you for your support!

Sammy

0
Hi Samy,

It's a pleasure to help. Good, your .txt is readable now!
0

Please sign in to leave a comment.