从utf-8文本文件读取文件路径?

发布于 2024-12-10 14:56:43 字数 1512 浏览 0 评论 0原文

我有一个 UTF-8 文本文件 example.txt,其中包含: c:/temp/file.txt

我使用这种方法读取文件内容:

public static String fileToString(final File file, final String charset) throws AppServerException
    {
        final byte[] buffer = new byte[(int) file.length()];
        FileInputStream fileInputStream = null;
        try
        {
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(buffer);
        }
        catch (final FileNotFoundException e)
        {
            throw new AppServerException(e.getMessage());
        }
        catch (final IOException e)
        {
            throw new AppServerException(e.getMessage());
        }
        finally
        {
            FileHelper.close(fileInputStream);
        }

        try
        {
            return new String(buffer,charset);
        }
        catch (UnsupportedEncodingException e)
        {
                throw new AppServerException(e.getMessage());
        }

    }

然后我想检查文件 c:/temp/file.txt 是否存在

String content = fileToString("example.txt","UTF8");
File file = new File(content );
System.out.println(file.exists());

exits() 返回 false 但文件确实存在。

如果我使用notepad++将example.txt的编码更改为ANSI,则exists()返回true。

我已经尝试使用: “c:\temp\file.txt”, “c:\\temp\\file.txt”, “c:\\\\temp\\\\file.txt”, 但没有成功。

我确实需要使用 UTF8 格式的文件。你有什么技巧可以使方法exists()返回true吗?

I have a UTF-8 text file example.txt that contains:
c:/temp/file.txt

I read the file content using this method:

public static String fileToString(final File file, final String charset) throws AppServerException
    {
        final byte[] buffer = new byte[(int) file.length()];
        FileInputStream fileInputStream = null;
        try
        {
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(buffer);
        }
        catch (final FileNotFoundException e)
        {
            throw new AppServerException(e.getMessage());
        }
        catch (final IOException e)
        {
            throw new AppServerException(e.getMessage());
        }
        finally
        {
            FileHelper.close(fileInputStream);
        }

        try
        {
            return new String(buffer,charset);
        }
        catch (UnsupportedEncodingException e)
        {
                throw new AppServerException(e.getMessage());
        }

    }

Then I want to check if the file c:/temp/file.txt exists:

String content = fileToString("example.txt","UTF8");
File file = new File(content );
System.out.println(file.exists());

The exits() return false but the file actually exists.

If I change the encoding of example.txt to ANSI using notepad++, the exists() return true.

I already tried using:
"c:\temp\file.txt",
"c:\\temp\\file.txt",
"c:\\\\temp\\\\file.txt",
but without success.

I really need to use the file as UTF8. Do you have tips so the method exists() returns true?

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

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

发布评论

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

评论(2

茶底世界 2024-12-17 14:56:43

Notepad++ 可能会在文件前面放置一个 字节顺序标记。对于 UTF-8 来说这是不必要的,Java 不会解释这个三个字符的序列

如果您的文件名不包含任何非 ASCII 字符,请使用不使用字节顺序标记的编辑器或以 ANSI 编写字符串。

Notepad++ probably puts a Byte Order Mark in front of the file. This is unnecessary for UTF-8 and Java does not interpret this sequence of three characters.

Either use an editor that does not use a Byte Order Mark or write the string in ANSI if your filename does not contain any non-ASCII characters.

傾旎 2024-12-17 14:56:43

也许该文件实际上并未编码为 UTF-8。你能打印文件中“\”字符的实际字节值吗?

当您执行此操作时:InputStream.read(byte[] b)保证从流中读取b.length字节。您应该循环读取并检查 read() 方法的返回值,以便了解每次调用中实际读取了多少字节。

Perhaps the file is not actually encoded as UTF-8. Can you print the actual byte values of the "\" characters in the file?

While you are at it: InputStream.read(byte[] b) is not guaranteed to read b.length bytes from the stream. You should be reading in a loop and checking the return value of the read() method in order to see how many bytes were actually read in each call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文