Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从android中的资产文件夹读取文本文件_Java_Android - Fatal编程技术网

Java 从android中的资产文件夹读取文本文件

Java 从android中的资产文件夹读取文本文件,java,android,Java,Android,可能重复: 嘿,我正在尝试从android的资产文件夹中读取一个文件,这就是我目前所拥有的 public class TestingMusicDAO { private static final String TAG_NAME = "MUSIC_TESTING_DAO"; private static List<Song> songs; private ContentResolver contentResolver; private static Context testingc

可能重复:

嘿,我正在尝试从android的资产文件夹中读取一个文件,这就是我目前所拥有的

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");
    }


}

}
公共类测试音乐DAO{
私有静态最终字符串标记\u NAME=“MUSIC\u TESTING\u DAO”;
私有静态列表歌曲;
私有ContentResolver ContentResolver;
私有静态上下文测试上下文;
私人档案室;
InputStream InputStream=null;
公共测试MusicDAO(上下文)引发IOException{
Log.d(标签名称,“设置测试歌曲”);
contentResolver=context.getContentResolver();
testingcontext=上下文;
getAllSongsFromFile();
}
公共静态void getAllSongsFromFile()引发IOException{
Log.d(TAG_NAME,“尝试获取所有歌曲”);
输入流为;
is=testingcontext.getAssets().open(“testing”);
Log.d(TAG_NAME,“那有用吗?”);
BufferedReader bufferReader=新的BufferedReader(新的InputStreamReader(is));
字符串输入线;
而((inputLine=bufferReader.readLine())!=null){
进程线(输入线);
}
bufferReader.close();
}
私有静态void logSongs(){
用于(歌曲:歌曲)
Log.d(TAG_NAME,song.toString());
}
公共列表getAllSongs(){
返回歌曲;
}
公共静态void processLine(字符串aLine){
扫描仪=新扫描仪(aLine);
scanner.useDelimiter(“=”);
if(scanner.hasNext()){
字符串标题=scanner.next();
字符串艺术家=scanner.next();
字符串相册=scanner.next();
字符串id=scanner.next();
字符串albumId=scanner.next();
String trackOrder=scanner.next();
Log.d(标记名称,“Title=“+Title+”Artist=“+Artist+”Album=“+Album+”ID=“+ID+”AlbumID=“+AlbumID”);
}
否则{
Log.d(TAG_NAME,“空行或无效行,无法处理”);
}
}
}
因此,我基本上调用TestingMusicDAO构造函数,从那里我想分别读取文件的每一行,以便能够解析它们,但它一直给我一个FileNotFoundExecution:testing。任何想法都很好,谢谢


Peter,

假设异常来自
is=testingcontext.getAssets().open(“testing”)行,则在
资产/
文件夹的根目录中没有名为
测试的文件。

假设异常来自
is=testingcontext.getAssets()。打开(“测试”)行,则在
资产/
文件夹的根目录中没有名为
测试的文件。

查看
http://stackoverflow.com/questions/9674815/trouble-with-reading-file-from-assets-folder-in-android
-您需要执行
getAssets()
也许您需要test.txt之类的扩展看看
http://stackoverflow.com/questions/9674815/trouble-with-reading-file-from-assets-folder-in-android
-您需要执行
getAssets()
也许您需要test.txt之类的扩展