Skip to content Skip to sidebar Skip to footer

Android Reading From A Text File

I have a java class where it reads some data from a text file using a buffered reader and returns that data as a hash map: import java.io.BufferedReader; import java.io.FileNotFoun

Solution 1:

without a bit of logcat it is a bit trivial.

 unigramFrequencies.put(splittedLine[0].trim(), Double.parseDouble(splittedLine[1].trim()))

here for instance could be raised a null pointer execption if splittedLine[0] or splittedLine[1] is null, or parseDouble could arise a number format execption

Solution 2:

I think the error might well be there :

BufferedReaderbf=newBufferedReader(newFileReader("unigramFrequencies.txt"));

You should provide an absolute path here and first make sure that the file exists before accessing it or handle the exception.

If this file is some final asset, you should place it in your project assets folder and get a filereader from there.

Example (from here):

AssetFileDescriptordescriptor= getAssets().openFd("unigramFrequencies.txt");
FileReaderreader=newFileReader(descriptor.getFileDescriptor());

Note that your unigramFrequencies.txt file should be present in your <project>/assets/ directory

Solution 3:

This is searching for a needle in the hay stack.

I recommend you to first learn how to use debugging in Android: http://www.droidnova.com/debugging-in-android-using-eclipse,541.html

Also some exception handling wouldn't hurt: http://en.wikibooks.org/wiki/Java_Programming/Throwing_and_Catching_Exceptions

The following line of code is very wrong, and it seems you don't understand file storage in android:

newFileReader("unigramFrequencies.txt")

Here it is explained: http://developer.android.com/guide/topics/data/data-storage.html

Post a Comment for "Android Reading From A Text File"