7

My code is following::

Scanner sc = null; try { sc = new Scanner(new File("assets/mainmenu/readSourceFile.txt")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block System.out.println(e1.toString()); e1.printStackTrace(); } 

Then, logcat show exception java.io.FileNotFoundException

How can I find my file? I tried

sc = new Scanner(new File("mainmenu/readSourceFile.txt"));

However, it's still throw FilenotFoundException

My file is in folder assets/mainmenu/readSourceFile.txt

and I've try this::

private InputStream is; try { is = this.getResources().getAssets().open("mainmenu/readSourceFile.txt"); } catch (IOException e) { e.toString(); } 

I use getAssets to access assets file in android. However, how can I suppose to read text file if I use InputStream

4
  • Is all your directories are located in the same project ? if so just give your path as ./assets/mainmenu/readSourceFile.txt.. Commented Dec 21, 2012 at 4:17
  • @barssala your text file is in asset folder? Commented Dec 21, 2012 at 4:17
  • sure, my text file is in assets/mainmenu/readSourceFile.txt Commented Dec 21, 2012 at 4:21
  • ./assets/mainmenu/readSourceFile.txt got Exception java.io.FileNotFoundException:/./asset/mainmenu/readSourceFile.txt Commented Dec 21, 2012 at 4:24

3 Answers 3

1

You try this...Hope it will work

sc = new Scanner(new File("file:///android_asset/mainmenu/readSourceFile.txt"); 

Edit try this

AssetFileDescriptor descriptor = getAssets().openFd("mainmenu/readSourceFile.txt"); FileReader reader = new FileReader(descriptor.getFileDescriptor()); 

Copied from here Asset folder path

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, but i got FileNotFoundException
for more information. java.io.FileNotFoundException:/file:/android_asset/mainmenu/readSourceFile.txt
0

You can't access the assets folder of your Android application as if they were regular files on the file system (hint: they're not). Instead, you need to use AssetManager.Call getAssets() on your activity/application context to get the AssetManager.

Once you have this, you can use open("mainmenu/readSourceFile.txt") to obtain an InputStream.

You can now read this InputStream like you would any other. Apache Commons IO is a great library for working with input and output streams. Depending on how you want to get the data, you could try:

  • Read the entire file into a String:

    try { String contents = IOUtils.toString(in); } finally { IOUtils.closeQuietly(in); } 
  • Read the entire file into a list of Strings, one per line:

    try { List<String> lines = IOUtils.readLines(in); } finally { IOUtils.closeQuietly(in); } 
  • Iterate through the file one line at a time:

    try { LineIterator it = IOUtils.lineIterator(in, "UTF-8"); while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { IOUtils.closeQuietly(in); } 

2 Comments

I tried InputStream and I can open file now. But I don't know how to read text files if I use InputStream
Apache Commons IO makes this easy. Download commons-io-2.4.jar and add it to your Java Build Path, then you can read the file in one shot: String mContents = IOUtils.toString(mInputStream). This link also shows how to do the same thing "manually" (InputStream -> InputStreamReader -> BufferedReader -> loop) if you don't want to use IOUtils for some reason...
0

I had the same issue. I noticed that the file should not include the hierarchy. This worked for me if someone had the same issue in future. `

 InputStream is=null; try { is=activity.getAssets().open("fileInAssets.dat"); } catch (IOException e1) { Log.e(TAG, "Erro while openning hosts file."); e1.printStackTrace(); } 

`

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.