0

i have an very strange behavoir in my android app.

The user have three ways to write a file: Excel, txt and pdf.

In my string it is possible that i have character just like this: "ä", "ß".

For the excel and txt File i use the following code to write:

FileWriter fWriter;
        try
        {
            String extr = sdCard.toString();
            File mFolder = new File(extr + "/" + FileUtil.TEST_DICTONARY);
            if (!mFolder.exists())
            {
                mFolder.mkdir();
            }

            File mFile = new File(mFolder.getAbsolutePath(), FileUtil.TEXT_FILENAME);

            mFile.delete();

            String appDataToWrite = createAppDataToWriteToFile(appData, resources);

            fWriter = new FileWriter(mFile, false);
            fWriter.write(appDataToWrite);
            fWriter.flush();
            fWriter.close();
        }
        catch (NotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

This works perfect, the encoding is i excepted. If i use the same for the pdf file then it's not the right encoding. After searching in SO i use the following:

try
        {
            String extr = sdCard.toString();
            File mFolder = new File(extr + "/" + FileUtil.TEST_DICTONARY);
            if (!mFolder.exists())
            {
                mFolder.mkdir();
            }

            File mFile = new File(mFolder.getAbsolutePath(), FileUtil.PDF_FILENAME);

            mFile.delete();

            String appDataToWrite = createAppDataToWriteToPdf(appData, resources);

            FileOutputStream fos = new FileOutputStream(mFile, false);
            OutputStreamWriter char_output = new OutputStreamWriter(fos, Charset.forName("UTF-8").newEncoder());
            char_output.write(appDataToWrite);
            char_output.flush();
            char_output.close();

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

I have no idea why this getting wrong. For your information, I'm using the following library for generating the pdf http://coderesearchlabs.com/androidpdfwriter/

I have debug the code and a can see that the OutputStreamWriter getting the right input.

Any suggestions?

Thanks, Manu

Manu Zi
  • 2,300
  • 6
  • 33
  • 67
  • 2
    "If i use the same for the pdf file then it's not the right encoding." PDFs aren't text files - they're binary files. Your `createAppDataToWriteToPdf` should return a byte array, not a String. – Jon Skeet Mar 22 '14 at 10:45
  • 1
    What is more, Excel sucks at reading UTF-8; it violates the Unicode specification in that it will not be able to read UTF-8 if there isn't a BOM. – fge Mar 22 '14 at 10:47
  • Also, you fail to check for the return code of `.mkdir()` – fge Mar 22 '14 at 10:48
  • @JonSkeet i have change the return type to the following: s.getBytes("UTF-8") but it is the same. – Manu Zi Mar 22 '14 at 10:55
  • See [here](http://stackoverflow.com/a/22576484/1093528). I believe there is the need for a FAQ entry somewhere about this problem, it is very common. – fge Mar 22 '14 at 10:57
  • @ManuZi: No, you've missed the point. You shouldn't be constructing a string at all - it's binary data. – Jon Skeet Mar 22 '14 at 11:14

0 Answers0