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,328
  • 9
  • 51
  • 90
Milos Cuculovic
  • 18,933
  • 50
  • 154
  • 263
  • 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
  • 89,914
  • 16
  • 195
  • 259
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
  • 130,641
  • 51
  • 193
  • 212
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
  • 19,939
  • 11
  • 82
  • 96
SBJ
  • 3,999
  • 2
  • 22
  • 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
  • 413
  • 1
  • 6
  • 9