【问题标题】:fileinputstream is not reading文件输入流没有读取
【发布时间】:2017-11-26 12:27:08
【问题描述】:

我已经尝试过这里发布的许多版本,但没有一个有效,所以我自己问。在我的代码中,我有这个公共功能。该文件存在并有一个文本,但该函数不返回任何内容。

我做错了什么,我必须改变什么以及如何改变?

public String lesen(String dateiname) throws IOException {
        FileInputStream fin = openFileInput(dateiname); // z.b.  "Montag.txt"
        int size;
        String neuText = null;
        while ((size = fin.read()) != -1) {
            neuText += Character.toString((char) size);
        }
        return neuText;
    }

【问题讨论】:

  • "文件存在" -- 你怎么知道? openFileInput() 打开internal storage 中的文件,普通用户是看不到的。您是否使用openFileOutput() 创建此文件?此外,一次读取一个字符的文件非常很慢,并且有更好的 Java 解决方案可以从 InputStream 读取文本。

标签: android fileinputstream


【解决方案1】:

尝试类似:

public String readAllLinesFrom(String file) throws IOException {
        StringBuilder lines = new StringBuilder();
        try(BufferedReader input = new BufferedReader(new FileReader(file))) {
            String row = null;
            while ((row = input.readLine()) != null) {
                lines.append(row);
            }
        }
        return lines.toString();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 2016-11-24
    • 2023-04-04
    • 2019-04-10
    • 2012-02-06
    相关资源
    最近更新 更多