0
  private void CopyAssets2() {
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("File Error", e.getMessage());
    }
    for (String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            out = new FileOutputStream("/sdcard/Translate/" + filename);
            copyFile2(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e) {
            Log.e("Save Error", e.getMessage());
        }
    }
}

private void copyFile2(InputStream in, OutputStream out)
            throws IOException {
    char[] buffer = new char[1024];
    Reader reader = new BufferedReader( new InputStreamReader(in, "UTF-8"));

    Writer writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));

    int read;
    while ((read = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, read);
    }
    reader.close();
    writer.flush();
    writer.close();
}

Im getting the inputstream with assetManager, and passing it as a parameter of reader with UTF-8 encoding specified. I'm also doing writing to the outputstream filepath with writer in UTF-8

The file is read and written, but the encoding is still wrong. I get characters like these: Where are... = �D�nde est� / D�nde

What I am doing wrong?

Evans Kakpovi
  • 312
  • 1
  • 5
  • 12
  • provided you solution here [UTF-8 formats reading text file][1] [1]: http://stackoverflow.com/questions/9946817/why-does-this-bufferedreader-not-read-in-the-specified-utf-8-format/22062191#22062191 – Muhammad Usman Ghani Feb 27 '14 at 07:59

1 Answers1

0

Are you sure the input file is encoded in UTF-8? The � you see in the output is a character that is used as a replacement for byte sequences that could not be converted into characters when reading.

You could make a binary copy instead of decoding and encoding text:

byte[] buffer = new byte[1024];

InputStream reader = new BufferedInputStream(in);

OutputStream writer = new BufferedOutputStream(out);

int read;
while ((read = reader.read(buffer)) != -1) {
    writer.write(buffer, 0, read);
}
Joni
  • 108,737
  • 14
  • 143
  • 193