从文本文件获取内容到字符串(Android)(FileNotFound)(Java)

从文本文件获取内容到字符串(Android)(FileNotFound)(Java),java,android,filenotfoundexception,Java,Android,Filenotfoundexception,我想从我的项目(android studio)中的文本文件中获取文本,并将该文本转换为字符串。我现在找不到正确的路径或其他东西。我使用在Stackoverflow上找到的两种方法将文本文件转换为字符串。方法如下: public static String convertStreamToString(InputStream is) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamRea

我想从我的项目(android studio)中的文本文件中获取文本,并将该文本转换为字符串。我现在找不到正确的路径或其他东西。我使用在Stackoverflow上找到的两种方法将文本文件转换为字符串。方法如下:

public static String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line).append("\n");
    }
    reader.close();
    return sb.toString();
}

public static String getStringFromFile (String filePath) throws Exception {
    File fl = new File(filePath);
    FileInputStream fin = new FileInputStream(fl);
    String ret = convertStreamToString(fin);
    //Make sure you close all streams.
    fin.close();
    return ret;
}
我调用了这样的方法,我尝试了各种路径,但似乎都不起作用:

Log.i("er0r", Solve.getStringFromFile("\\tables\\lowerLayer\\cross\\whiteRed.txt"));
这只是试图打印文本文件的内容。我得到以下错误:java.io.FileNotFoundException:。\tables\lowerLayer\cross\whiteRed.txt:open failed:enoint(没有这样的文件或目录)

以下是我订购包裹的方式:

我怎样才能解决这个问题?谢谢

编辑:

使用infle=“assets\whiteRed.txt”尝试此操作 告诉我这个错误:java.io.FileNotFoundException:assets\whiteRed.txt

附加代码: 调用LoadData方法的类的构造函数

public class Solve {

private Context context;
//constructor

public Solve(Context context){
    this.context = context;
}

如果在设计时,在Android Studio中,您希望提供应用程序在运行时可以读取的文件,然后将其放入Android Studio项目的资产文件夹中

也许你必须先创建资产文件夹

之后,您的应用程序可以使用资产管理器从资产中读取这些文件


只有谷歌知道如何做到这一点。所有内容都已在此处发布多次。

(“\\tables\\lowerLayer\\cross\\whiteRed.txt”
。这是一个不存在的不可能的文件系统路径。难怪找不到该文件。你是如何找到该路径的?你的txt文件实际在哪里?在我链接的图片中,你可以看到它的结构。我对这一点很陌生,正确的路径是什么?谢谢Android Studio中的路径。你认为它们为什么会出现在你的系统中例如,当你的手机不在城里时,应用程序?嗯,我想所有这些文件都会进入apk或其他地方。如果没有,我如何才能将这些文件进入手机?谢谢
尝试使用infle=“assets\whiteRed.txt”
。否。尝试使用
infle=“whiteRed.txt”
我没有发现任何有用的东西…我已经创建了一个资产目录,但无法使其工作。我们将您在帖子中尝试过的代码发布出来。然后告诉您困难所在。从资产目录中的一个文件开始,以保持简单。对于这样的混乱,我深表歉意。
public class Solve {

private Context context;
//constructor

public Solve(Context context){
    this.context = context;
}