从utf-8文本文件读取文件路径?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
也许该文件实际上并未编码为 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 readb.length
bytes from the stream. You should be reading in a loop and checking the return value of theread()
method in order to see how many bytes were actually read in each call.