0

I am using FileUtils.readFileToString to read contents of a text file with JSON at once. The file is UTF-8 encoded (w/o BOM). Yet, instead of cyrillic letters I get ?????? signs. Why?

public String getJSON() throws IOException
{
    File customersFile = new File(this.STORAGE_FILE_PATH);
    return FileUtils.readFileToString(customersFile, StandardCharsets.UTF_8);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Mikhail Batcer
  • 1,938
  • 7
  • 37
  • 57
  • 3
    Where do you see ??? instead of cyrilic? Console? IDE? Other file? – StephaneM Feb 12 '15 at 13:35
  • @StephaneM that question should have basically answered his question :D – nafas Feb 12 '15 at 13:36
  • @StephaneM I have built a small REST service using Spring Framework. ????s are returned in response for GET request. Yet they are only in place of cyrillic letters. – Mikhail Batcer Feb 12 '15 at 13:37
  • You don't answer my question. Where do you read the response of your GET request? – StephaneM Feb 12 '15 at 13:39
  • @StephaneM In any place where I can make GET request: from a browser, from a Javascript console, from a Python console. – Mikhail Batcer Feb 12 '15 at 13:41
  • It looks like this: {"lastName":"??????","phone":"77777777777","email":"test@example.com","address":"??????","secondName":"???????","firstName":"???????","city":"??????"} – Mikhail Batcer Feb 12 '15 at 13:43
  • 1
    And does the font in the browser have glyphs for cyrillic chars (and encoding set to UTF-8)? – Klas Lindbäck Feb 12 '15 at 13:51
  • @KlasLindbäck Yes, it does. – Mikhail Batcer Feb 12 '15 at 13:56
  • What is the encoding of the REST response? You need to examine every and all places where strings are converted to/from bytes: reading the file on server, sending data from server to client, saving data on client, viewing data on client. – Andreas Mar 05 '20 at 01:10

3 Answers3

0

FileUtils.readFileToString doesn't work with StandardCharsets.UTF_8.

Instead, try

FileUtils.readFileToString(customersFile, "UTF-8");

or

FileUtils.readFileToString(customersFile, StandardCharsets.UTF_8.name());

Guillaume F.
  • 1,010
  • 7
  • 21
  • 1
    Well, there is a variant of `FileUtils.readFileToString` which accepts `Charset`, so why not `StandardCharsets.UTF_8`? I have solved the problem another way long ago, but in my free time I'll maybe take a look again. – Mikhail Batcer Mar 05 '20 at 01:06
  • Agree with @MikhailBatcer , see https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html#readFileToString(java.io.File,%20java.nio.charset.Charset) – pushNpop Feb 03 '22 at 16:19
0

This is how I solved it back in 2015:

public String getJSON() throws IOException
{
//    File customersFile = new File(this.STORAGE_FILE_PATH);
//    return FileUtils.readFileToString(customersFile, StandardCharsets.UTF_8);
    String JSON = "";
    InputStream stream = new FileInputStream(this.STORAGE_FILE_PATH);
    String nextString = "";
    try {
        if (stream != null) {
            InputStreamReader streamReader = new InputStreamReader(stream, "UTF-8");
            BufferedReader reader = new BufferedReader(streamReader);
            while ((nextString = reader.readLine()) != null)
                JSON = new StringBuilder().append(JSON).append(nextString).toString();
        }
    }
    catch(Exception ex)
    {
        System.err.println(ex.getMessage());
    }
    return JSON;
}
Mikhail Batcer
  • 1,938
  • 7
  • 37
  • 57
0

I figured out that in logs all was fine, so I solved problem in rest controller:

 @GetMapping(value = "/getXmlByIin", produces = "application/json;charset=UTF-8")