Android java.io.FileNotFoundException用于文本文件

Android java.io.FileNotFoundException用于文本文件,android,filereader,Android,Filereader,我的Android项目根文件夹中有一个名为KARIN.txt的txt文件。我正试图通过文件阅读器访问它,但出现以下错误: java.io.FileNotFoundException: /KARIN.txt: open failed: ENOENT (No such file or directory) 我试图访问该文件的方式如下: BufferedReader br = new BufferedReader(new FileReader("KARIN.txt")); 我不明白为什么它不允许我

我的Android项目根文件夹中有一个名为KARIN.txt的txt文件。我正试图通过
文件阅读器
访问它,但出现以下错误:

java.io.FileNotFoundException: /KARIN.txt: open failed: ENOENT (No such file or directory)
我试图访问该文件的方式如下:

BufferedReader br = new BufferedReader(new FileReader("KARIN.txt"));

我不明白为什么它不允许我读取该文件。

如果KARIN.txt已内置到您的项目中,您应该将其放在assets文件夹中,并通过AssetManager访问它

public String loadFile(String file){
        AssetManager am = getActivity().getAssets();
        InputStreamReader ims = null;
        BufferedReader reader = null;
        String data = "File not available!";
        try {
            ims = new InputStreamReader(am.open(file), "UTF-8");
            reader = new BufferedReader(ims);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(ims != null){
            try {
                String mLine = reader.readLine();
                data = "";
                while(mLine != null){
                    data+= mLine;
                    mLine = reader.readLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                if (reader != null) {
                     try {
                         reader.close();
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                }
            }
        }
        return data;
}

如果项目中内置了KARIN.txt,则应将其放在assets文件夹中,并通过AssetManager进行访问

public String loadFile(String file){
        AssetManager am = getActivity().getAssets();
        InputStreamReader ims = null;
        BufferedReader reader = null;
        String data = "File not available!";
        try {
            ims = new InputStreamReader(am.open(file), "UTF-8");
            reader = new BufferedReader(ims);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(ims != null){
            try {
                String mLine = reader.readLine();
                data = "";
                while(mLine != null){
                    data+= mLine;
                    mLine = reader.readLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                if (reader != null) {
                     try {
                         reader.close();
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                }
            }
        }
        return data;
}

如果项目中内置了KARIN.txt,则应将其放在assets文件夹中,并通过AssetManager进行访问

public String loadFile(String file){
        AssetManager am = getActivity().getAssets();
        InputStreamReader ims = null;
        BufferedReader reader = null;
        String data = "File not available!";
        try {
            ims = new InputStreamReader(am.open(file), "UTF-8");
            reader = new BufferedReader(ims);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(ims != null){
            try {
                String mLine = reader.readLine();
                data = "";
                while(mLine != null){
                    data+= mLine;
                    mLine = reader.readLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                if (reader != null) {
                     try {
                         reader.close();
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                }
            }
        }
        return data;
}

如果项目中内置了KARIN.txt,则应将其放在assets文件夹中,并通过AssetManager进行访问

public String loadFile(String file){
        AssetManager am = getActivity().getAssets();
        InputStreamReader ims = null;
        BufferedReader reader = null;
        String data = "File not available!";
        try {
            ims = new InputStreamReader(am.open(file), "UTF-8");
            reader = new BufferedReader(ims);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(ims != null){
            try {
                String mLine = reader.readLine();
                data = "";
                while(mLine != null){
                    data+= mLine;
                    mLine = reader.readLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                if (reader != null) {
                     try {
                         reader.close();
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                }
            }
        }
        return data;
}


可能是因为该确切位置没有文件,您已经检查过了吗?您不能在
/
上写入。只有root用户才能在
/
上写入,请尝试指定我的Android项目的绝对path.root文件夹。它在你的电脑上吗?如果我把KARIN.txt放在文本文件夹里呢。我应该如何使用FileReader调用它?可能是因为在该确切位置没有文件,您已经检查过了吗?您不能在
/
上写入。只有root用户才能在
/
上写入,请尝试指定我的Android项目的绝对path.root文件夹。它在你的电脑上吗?如果我把KARIN.txt放在文本文件夹里呢。我应该如何使用FileReader调用它?可能是因为在该确切位置没有文件,您已经检查过了吗?您不能在
/
上写入。只有root用户才能在
/
上写入,请尝试指定我的Android项目的绝对path.root文件夹。它在你的电脑上吗?如果我把KARIN.txt放在文本文件夹里呢。我应该如何使用FileReader调用它?可能是因为在该确切位置没有文件,您已经检查过了吗?您不能在
/
上写入。只有root用户才能在
/
上写入,请尝试指定我的Android项目的绝对path.root文件夹。它在你的电脑上吗?如果我把KARIN.txt放在文本文件夹里呢。我应该如何用FileReader调用它?Eclipse将getActivity下划线为红色。啊,很抱歉,这是我从另一个片段中得到的代码。在本例中,忽略getActivity(),只需使用
getAssets()
Eclipse将getActivity下划线为红色。啊,很抱歉,这是另一个片段中的代码。在本例中,忽略getActivity(),只需使用
getAssets()
Eclipse将getActivity下划线为红色。啊,很抱歉,这是另一个片段中的代码。在本例中,忽略getActivity(),只需使用
getAssets()
Eclipse将getActivity下划线为红色。啊,很抱歉,这是另一个片段中的代码。在这种情况下,忽略getActivity(),只需使用
getAssets()