Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.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
Can';在java中读取文本文件(Android studio)_Java_Android_File - Fatal编程技术网

Can';在java中读取文本文件(Android studio)

Can';在java中读取文本文件(Android studio),java,android,file,Java,Android,File,我试着寻找像我这样的问题,发现了很多,但没有一个答案对我有用。 我正在使用Android studio并尝试从java类打开文本文件,但无论我做什么或如何打开,我都会遇到以下错误: “…打开失败:enoint(没有这样的文件或目录)” 如您所见,我还尝试了两种选择: 1.创建一个“File”类(然后在watchs窗口中,我试图调用--“canRead()”函数,但返回“false”值)。 2.尝试向“FileReader”类的ctor发送我的文件路径。 他们中没有人工作 非常感谢 pub

我试着寻找像我这样的问题,发现了很多,但没有一个答案对我有用。 我正在使用Android studio并尝试从java类打开文本文件,但无论我做什么或如何打开,我都会遇到以下错误: “…打开失败:enoint(没有这样的文件或目录)”

如您所见,我还尝试了两种选择: 1.创建一个“File”类(然后在watchs窗口中,我试图调用--“canRead()”函数,但返回“false”值)。 2.尝试向“FileReader”类的ctor发送我的文件路径。 他们中没有人工作

非常感谢

    public void fillDatabase(){
    File file = new File("C:\\fillStops.txt");

    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("DELETE FROM " + STOPS_TABLE_NAME);
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        FileReader fileReader = new FileReader("C:\\fillStops.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuffer stringBuffer = new StringBuffer();
        String line;

        while ((line = bufferedReader.readLine()) != null) {
            this.addStop(line);
        }
        fileReader.close();
    }
    catch (Exception e){
        String s = e.getMessage();
    }
}

尝试此操作,并将文本文件放在SD卡的根目录下,否则请在以下代码中更改文件的路径

File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

//Read text from file
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;
    String aDataRow = "";

        while ((aDataRow = br.readLine()) != null) {
            line+= aDataRow + "\n";
        }

 Log.d(">>>>>", line);
}
catch (IOException e) {
    //You'll need to add proper error handling here
}
希望它能帮你解决问题。:)

附言


不要忘记将
权限添加到您的清单中。

可能是因为您的android设备没有
C:\
驱动器…omg omg omg。。。救我们吧……哈哈哈,真有趣这不是windows,伙计。