将内容从文本文件获取到字符串(Android)(FileNotFound)(Java) [英] Get content from a text file to String (Android) (FileNotFound) (Java)

查看:141
本文介绍了将内容从文本文件获取到字符串(Android)(FileNotFound)(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我在项目(android studio)中拥有的文本文件中获取文本,并将该文本转换为字符串.我目前无法获得正确的路径或其他东西.我使用在Stackoverflow上找到的两种方法来将文本文件转换为Strings.这些是方法:

I want to get the text from a text file I have in my project (android studio) and make that text to a string. I am currently having trouble getting the correct path or something. I'm using two methods I found here on Stackoverflow to get the textfiles to Strings. These are the methods:

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

我正在调用这样的方法,并且我尝试了各种路径,但似乎都不起作用:

And I'm calling the methods like this, and I have tried all kinds of pathing but none seems to work:

Log.i("er0r", Solve.getStringFromFile("\\tables\\lowerLayer\\cross\\whiteRed.txt"));

这只是一个尝试打印文本文件内容的尝试.我收到以下错误:java.io.FileNotFoundException:.\ tables \ lowerLayer \ cross \ whiteRed.txt:打开失败:ENOENT(没有这样的文件或目录)

This is just an attempt to print the content of the textfile. I get the following error: java.io.FileNotFoundException: .\tables\lowerLayer\cross\whiteRed.txt: open failed: ENOENT (No such file or directory)

这是我订购包裹的方式: http://imgur.com/a/rK9R5

This is how I have ordered my packages: http://imgur.com/a/rK9R5

我该如何解决?谢谢

public String LoadData(String inFile) {
    String str = "";
    try{
        StringBuilder buf=new StringBuilder();
        InputStream json=getAssets().open(inFile);
        BufferedReader in=
                new BufferedReader(new InputStreamReader(json, "UTF-8"));
        while ((str=in.readLine()) != null) {
            buf.append(str);
        }
        in.close();
    } catch (Exception e) {
        Log.e("er0r", e.toString());
    }
    return str;
}

使用inFile ="assets \ whiteRed.txt"对此进行了尝试 给我这个错误:java.io.FileNotFoundException:assets \ whiteRed.txt

Tried this with inFile = "assets\whiteRed.txt" Got me this error: java.io.FileNotFoundException: assets\whiteRed.txt

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

ADDITIONAL CODE: Constructor of the class that's calling the LoadData method

public class Solve {

private Context context;
//constructor

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

推荐答案

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

If at design time, in Android Studio, you want to supply files which your app can read at run time then put them in the assets folder of your Android Studio project.

也许您必须先创建该资产文件夹.

Maybe you have to create that assets folder first.

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

After that your app can read those files from assets using assets manager.

仅Google提供具体操作方法.所有内容已多次发布在这里.

Just google for how to do this exactly. All has been posted here many times.

这篇关于将内容从文本文件获取到字符串(Android)(FileNotFound)(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆