25

I'm doing:

FileReader fin = new FileReader("file:///android_asset/myFile.txt");

in an Android project and many variations. At runtime I get a file not found exception. The file is present and correct in the assets folder, so my path must be wrong.

What is the absolute path I need here?

Ken
  • 30,811
  • 34
  • 116
  • 155

6 Answers6

47
AssetFileDescriptor descriptor = getAssets().openFd("myfile.txt");
FileReader reader = new FileReader(descriptor.getFileDescriptor());

Try using the above with FileDescriptors. Seems to be the most foolproof way I've found to gather asset paths.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • 5
    @kcoppock: Here Dalvik complains "FileNotFoundException", saying that the file's probably compressed. – Ken Jan 25 '11 at 03:46
  • You're SURE it's directly in the assets folder, not a subfolder? – Kevin Coppock Jan 25 '11 at 05:05
  • @kcoppock:It's directly in the assets folder. – Ken Jan 25 '11 at 05:48
  • @SK9: I don't think it's what's causing your problem, but I had a typo, it should be `getFileDescriptor()`, not `getDescriptor()`, but I'm assuming you already figured that one out... – Kevin Coppock Jan 25 '11 at 13:34
  • Can you post your trace? According to the docs, FileReader(FileDescriptor) cannot throw FileNotFoundException, and openFd(String) only throws IOException. – Kevin Coppock Jan 25 '11 at 13:35
  • @kcoppock: No problem at all, Eclipse picked up the typo for me. Thank you for following up. – Ken Jan 25 '11 at 22:49
  • @kcoppock: See edited question for trace. (Using descriptor.getFileDescriptor()) is also throwing me a java.io.FileNotFoundException (in the first line it says the file is probably compressed). But it's an asset? – Ken Jan 25 '11 at 22:54
  • Interesting. I wonder if it's an encoding issue or something with your particular file? Can you try just making a blank text document in your assets folder, and typing your text in there? Then see if you still have the problem. – Kevin Coppock Jan 26 '11 at 14:40
  • user1013512's below answer is the right one, the problem is that android compresses every file except ones like mp3, png... [this blogpost](http://ponystyle.com/blog/2010/03/26/dealing-with-asset-compression-in-android-apps/) explains that very well – Don Ho Mar 13 '12 at 17:42
  • I got the same file compressed error trying this with an ini. – BuvinJ Oct 06 '15 at 14:19
  • You can tell android not to compress certain file types: http://stackoverflow.com/a/33359656/2832027 – TTransmit Aug 08 '16 at 14:10
19
    InputStream is = getResources().getAssets().open("terms.txt");
    String textfile = convertStreamToString(is);

public static String convertStreamToString(InputStream is)
            throws IOException {
            Writer writer = new StringWriter();

            char[] buffer = new char[2048];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is,
                        "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            String text = writer.toString();
            return text;
    }
jayesh kavathiya
  • 3,531
  • 2
  • 22
  • 25
3

Can you use something like

    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(context.getAssets().open("fileName.txt")));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Nick Campion
  • 10,479
  • 3
  • 44
  • 58
  • This is what I'm currently doing. Using a buffered reader to read line by line is too slow for my needs. – Ken Jan 25 '11 at 03:43
  • 1
    Then instead of a BufferedReader use a bufferedInputStream. I would be surprised, however, if the buffered reader / buffered input stream is slowing down your application, since their very existence is to speed us I/O access to OS resources. – Nick Campion Jan 25 '11 at 03:49
  • Quick reply - thank you. I'll try this. Reading a text file line-by-line isn't helping, I need to read ahead, but it may not be the only reason. That I want to find out. – Ken Jan 25 '11 at 03:53
3

Its not reading it because all assets in assets folder are compressed, try changing its extension to .mp3 then read it in, that should stop it from being compressed.

panthro
  • 22,779
  • 66
  • 183
  • 324
2

I found that if you are using an IDE like Eclipse you may need to do a clean -- or delete the API in the bin directory. It seems that the assets isn't getting updated when you do a build.

1
AssetManager am = context.getAssets();
InputStream fs = am.open("myFile.txt");
user598417
  • 31
  • 2