Found reliance on default encoding: new java.io.FileWriter(File, boolean)
Asked Answered
S

4

7

I'm using FileWrite class to write into a file.and its working fine. But FindBugs is pointing me a Minor issue in my code snippet.

code snippet:

  SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd");
        Date now = new Date();
        String fileName = formatter.format(now) + ".txt";
        FileWriter writer = null;
        try {
            File root = new File(Environment.getExternalStorageDirectory(), "Test");
            if (!root.exists()) {
                root.mkdirs();
            }
            File gpxfile = new File(root, fileName);

            writer = new FileWriter(gpxfile, true);
            writer.append(text + "\n\n");

        } catch (IOException e) {
            e.printStackTrace();

        } finally {
            if (writer != null) {
                try {
                    writer.flush();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

Findbug Report:

Reliance on default encoding Found reliance on default encoding: new java.io.FileWriter(File, boolean)

In which line i'm getting this Error?

  writer = new FileWriter(gpxfile, true);

Could some one please brief me what is this exactly? And how can we solve this?

Solecism answered 13/7, 2018 at 7:19 Comment(0)
S
5

Resolved this Issue by replacing

FileWriter writer = new FileWriter(gpxfile, true);

with

  FileOutputStream fileStream = new FileOutputStream(gpxfile);
            writer = new OutputStreamWriter(fileStream, "UTF-8");
Solecism answered 13/7, 2018 at 7:33 Comment(0)
C
1
File file = new File(someFilePath);
Writer w = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
PrintWriter pw = new PrintWriter(w);
pw.println(someContent);
pw.close();
Chaise answered 28/5, 2019 at 17:52 Comment(1)
Welcome to StackOverflow. Can you please add some additional details to your answer? Although some code only answers are acceptable it is better to add an explanation for people who will read this in the future.Rational
R
1

If you are using FileWriter to write a file and you are worried about encoding. You can change the code to use FileWriterWithEncoding where you can specify encoding. With java 11 or above FileWriter comes with encoding to be used as an argument.

try (FileWriterWithEncoding fileWriter = new FileWriterWithEncoding(name, StandardCharsets.UTF_8,true)){

    fileWriter.append("xxx");
    ....
}
Rael answered 27/4, 2022 at 7:21 Comment(0)
P
0
import java.io.BufferedWriter;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;

Charset charSet = Charset.forName("UTF-8");
File file = new File(someFilePath);
BufferedWriter bufferedWriter = Files.newBufferedWriter(file.toPath(), charSet);
bufferedWriter.write(header);
bufferedWriter.close();
Pitchman answered 10/1, 2020 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.