Question

I know there are a lot of questions similar to this all around SO, but they either provide a very case-specific solution that I don't get to adapt to my issue or simply don't seem to work at all.

I have a multi-language app that downloads certain information from the internet and stores it into a file for later usage. This is how the storage is done:

public static void writeStringToFile(String string, File file)
        throws IOException {

    if (!file.exists()) {
        file.createNewFile();
    }

    FileOutputStream outputStream = new FileOutputStream(file);
    outputStream.write(string.getBytes("UTF-8"));
    outputStream.close();
}

But later, when the spanish version of the file is read, the app displays the special characters, like ñ, as the black diamond with the question mark inside I-ve tried to:

  • Download the information in my computer to check that the file is fine and put it manually in the app so that it reads from it instead of downloading it itself. The file is fine, but the app shows no change.
  • Replace the argument of getBytes by "ISO 8859-1", but the only difference in the result is that the weird character is this time the regular question mark.
  • Copy the file, once downloaded, from the device to the computer to check if it was fine, and it was already wrong (there are "empty square" characters shown in place of the question marks, that are not shown if I wget the file).

So I'm almost sure that the problem is in how I write the file since it gets out of the server fine but is stored wrong. But I have been so much time looking at the method and I can't find what the problem is...any clues?

EDIT: This is how I download the information.

    public static InputStream performGetRequest(String uri)
        throws IOException, URISyntaxException, ServerIsCheckingException {
    HttpResponse response;

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(new URI(uri));
    response = client.execute(request);
    if (response.getStatusLine().getStatusCode() == 409) {
        throw new ServerIsCheckingException();
    }
    else {
        return response.getEntity().getContent();
    }
}

To convert it to a String object that I later pass to the method writeStringToFile, I use

    public static String inputStreamAsString(InputStream is) throws IOException {
    java.util.Scanner s = new java.util.Scanner(is);
    String ret;
    ret = s.useDelimiter("\\A").hasNext() ? s.next() : "";
    return ret;
}

I also thought that writeStringToFile could be the problem, but I tried another alternative which specifies UTF-8 to be used and didn't work either.

Was it helpful?

Solution

You'll have to make sure that the document you are trying to write is being read in the same charset. In your case, if the document you're downliading is in spanish, it will probably be written in UTF-8 or ISO-8859-1, so you'll have to set the corresponding enconding both in the reading and writing.

You might use HttpProtocolParams.setContentCharset() to set the corresponding charset to the BasicHttpParams object.

This might help:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top