从android中的assets文件夹中读取文本文件

时间:2013-01-23 21:55:05

标签: java android

  

可能重复:
  Trouble with reading file from assets folder in Android

嘿我正在尝试从android中的assets文件夹中读取一个文件,这是我到目前为止所拥有的

public class TestingMusicDAO {

private static final String TAG_NAME = "MUSIC_TESTING_DAO";
private static List<Song> songs;
private ContentResolver contentResolver;
private static Context testingcontext;
private File fFile;
InputStream inputStream = null;


public TestingMusicDAO( Context context) throws IOException{
    Log.d(TAG_NAME, "Setting up testing songs");
    contentResolver = context.getContentResolver();
    testingcontext = context;
    getAllSongsFromFile();

}

public static void getAllSongsFromFile() throws IOException{
    Log.d( TAG_NAME, "Tryign to Get all Songs" );
    InputStream is;

    is = testingcontext.getAssets().open("testing");
    Log.d( TAG_NAME, "Did that work?" );
    BufferedReader bufferReader = new BufferedReader(new InputStreamReader(is));
    String inputLine;
    while((inputLine = bufferReader.readLine()) != null){
        processLine(inputLine);
    }
    bufferReader.close();

}

private static void logSongs() {
    for(Song song : songs)
        Log.d( TAG_NAME, song.toString() );
}

public List<Song> getAllSongs() {
    return songs;
}

public static void processLine(String aLine) {
    Scanner scanner = new Scanner(aLine);
    scanner.useDelimiter("=");
    if(scanner.hasNext()){
        String title = scanner.next();
        String artist = scanner.next();
        String album = scanner.next();
        String id = scanner.next();
        String albumId = scanner.next();
        String trackOrder = scanner.next();
        Log.d(TAG_NAME, "Title = " + title + "Artist = " + artist + "Album = " + album + "ID = " + id + "AlbumID = " + albumId);
    }
    else {
        Log.d(TAG_NAME, "Empty or invalid line. Unable to process");
    }


}

}

所以我基本上调用了TestingMusicDAO构造函数,并且从那里我想单独读取文件的每一行以便能够解析它们但它不断给我一个FileNotFoundExecption:testing。任何想法都会非常感谢!

彼得,

1 个答案:

答案 0 :(得分:1)

假设异常来自is = testingcontext.getAssets().open("testing");行,那么您的testing文件夹的根目录中没有名为assets/的文件。