从文件读取文本时出现问题

发布于 2024-11-18 07:15:27 字数 1113 浏览 3 评论 0原文

我正在尝试从 .txt 文件中读取一些文本,这是我的代码:

String filePath = bundle.getString("filepath");

        StringBuilder st = new StringBuilder();

        try {
            File sd = Environment.getExternalStorageDirectory();
            File f = new File(sd, filePath);
            FileInputStream fileis = new FileInputStream(f);
            BufferedReader buf = new BufferedReader(new InputStreamReader(
                    fileis));
            String line = new String();
            while ((line = buf.readLine()) != null) {
                st.append(line);
                st.append('\n');
            }
            Log.i("egor", "reading finished, line is " + line);
        } catch (FileNotFoundException e) {
            Log.i("egor", "file not found");
        } catch (IOException e) {
            Log.i("egor", "io exception");
        }

        reader.setText(st.toString());

文本如下所示:

这是要测试的示例文本

。.txt 文件是在 Windows 记事本中创建的。

这是我得到的:

在此处输入图像描述

我的代码有什么问题?提前致谢。

I'm trying to read some text from a .txt file, here's my code:

String filePath = bundle.getString("filepath");

        StringBuilder st = new StringBuilder();

        try {
            File sd = Environment.getExternalStorageDirectory();
            File f = new File(sd, filePath);
            FileInputStream fileis = new FileInputStream(f);
            BufferedReader buf = new BufferedReader(new InputStreamReader(
                    fileis));
            String line = new String();
            while ((line = buf.readLine()) != null) {
                st.append(line);
                st.append('\n');
            }
            Log.i("egor", "reading finished, line is " + line);
        } catch (FileNotFoundException e) {
            Log.i("egor", "file not found");
        } catch (IOException e) {
            Log.i("egor", "io exception");
        }

        reader.setText(st.toString());

The text looks like this:

This is a sample text to test

The .txt file is created in Windows notepad.

And here's what I'm getting:

enter image description here

What's wrong with my code? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你没皮卡萌 2024-11-25 07:15:27

文件是 utf-8 (unicode) 格式吗?由于某种原因,记事本总是向 unicode 文件添加字节顺序标记,即使字节顺序无关。当解释为 ASCII 或 ANSI 时,BOM 将被视为多个字符。这可能就是导致您出现问题的原因。

如果是这样,解决方案是使用比记事本更强大的文本编辑器,或者编写首先在所有 unicode 文件中检查 BOM 的代码。

如果这些对您来说都没有意义,请尝试谷歌搜索“unicode”和“字节顺序标记”。

Is the file in utf-8 (unicode) format? For some reason, Notepad always adds a byte-order mark to unicode files, even when the byte-order is irrelevant. When interpreted as ASCII or ANSI, the BOM will be seen as several characters. It's possible this is what's causing your problem.

If so, the solution is to use a more competent text editor than Notepad, or write code that checks for a BOM first in all unicode files.

If none of this makes sense to you, try googling 'unicode' and 'byte-order mark'.

赠佳期 2024-11-25 07:15:27

将 FileReader 对象包装在 BufferedReader 对象中。
http://download.oracle.com/javase /1.4.2/docs/api/java/io/FileReader.html

File sd = Environment.getExternalStorageDirectory();
File file = new File(sd, filePath);
BufferedReader br = new BufferedReader(new FileReader(file));

String line = "";

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

br.close();

Wrap a FileReader object in the BufferedReader object instead.
http://download.oracle.com/javase/1.4.2/docs/api/java/io/FileReader.html

File sd = Environment.getExternalStorageDirectory();
File file = new File(sd, filePath);
BufferedReader br = new BufferedReader(new FileReader(file));

String line = "";

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

br.close();
清晨说晚安 2024-11-25 07:15:27

尝试使用以下代码

File f = new File(str);
FileInputStream fis = new FileInputStream(f);
        byte[] mydata1 = new byte[(int) f.length()];
        fis.read(mydata1);
System.out.println("...data present in 11file..."+new String(mydata1));

Try with the folowing code

File f = new File(str);
FileInputStream fis = new FileInputStream(f);
        byte[] mydata1 = new byte[(int) f.length()];
        fis.read(mydata1);
System.out.println("...data present in 11file..."+new String(mydata1));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文