12

How can we fix the below issue reported by findBugs:

Found reliance on default encoding in abc.java : new java.io.FileReader(File)

I'm just reading file & Findbug has reported the issue.

Any help is much appreciated!

Mike
  • 7,606
  • 25
  • 65
  • 82

1 Answers1

14

use an explicit character encoding when opening a file instead of relying on the platform default (which can change depending on the platform), unless of course, you intend to use the platform default. you can use InputStreamReader to convert a FileInputStream to a Reader using an explicit character encoding.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • 3
    Can you provide some code snippet? – Mike Aug 15 '12 at 05:28
  • 2
    @Mike - a quick google search for "java FileReader character encoding" found [this](http://stackoverflow.com/questions/696626/java-filereader-encoding-issue) – jtahlborn Aug 15 '12 at 13:57
  • 8
    `InputStream inputStream = new FileInputStream(targetFile); Reader fileReader = new InputStreamReader(inputStream, "UTF-8");` is working. – prem30488 May 15 '17 at 10:49