6

I am trying to read a txt file from assets folder like that:

descriptor = context.getAssets().openFd("openAccess.txt");
reader = new FileReader(descriptor.getFileDescriptor());

but I am getting this exception:

java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed

I don't know what is the problem?

Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101
Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265
  • possible duplicate of [java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed](http://stackoverflow.com/questions/6186866/java-io-filenotfoundexception-this-file-can-not-be-opened-as-a-file-descriptor) – Sergey Glotov Apr 13 '12 at 13:17
  • ana,try using `AssetFileDescriptor` – ρяσѕρєя K Apr 13 '12 at 13:21
  • @imrankhan, how to use it? Thank you. – Milos Cuculovic Apr 13 '12 at 13:28
  • See me comment: http://stackoverflow.com/questions/6186866/java-io-filenotfoundexception-this-file-can-not-be-opened-as-a-file-descriptor/31425092#31425092 – Alfaplus Jul 15 '15 at 08:37

5 Answers5

3

How about this:

InputStream in = context.getAssets().open("openAccess.txt");
reader = new InputStreamReader(in);
David Wasser
  • 93,459
  • 16
  • 209
  • 274
2

try this :

AssetFileDescriptor descriptor = getAssets().openFd("openAccess.txt");
BufferedReader f = new BufferedReader(new FileReader(descriptor.getFileDescriptor()));
String line = f.readLine();
while (line != null) {
    // do stuff
    Log.d("TAG",line);
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

What did it for me was to create a "row"-folder in /res and to copy the files in there. Then you can use:

InputStreamReader iReader = new InputStreamReader(getResources().openRawResource(R.raw.text)));
beNick
  • 11
  • 1
0

Use like this. File Path.

context.getAssets().openFd("file:///android_asset/openAccess.txt");
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
SBJ
  • 4,089
  • 3
  • 24
  • 27
0

you can create new folder in Asset and place your file in that folder and try to get that file from this folder

noname
  • 423
  • 1
  • 6
  • 9