Fedora Eclipse - io 流中的空字符
我在 Eclipse 中遇到了一个非常奇怪的问题,我不确定它是否是一个错误或者某些设置是否错误。
我在 Fedora 14 上使用 Fedora Eclipse。
我第一次注意到这个问题是在 Eclipse 文本编辑器中打开包含空字符的文件时。到达第一个空字符后,不再显示任何字符。此外,如果文件在 null 之后包含新行,则不会显示 null 和换行符之间的所有内容,但会在换行符之后正确恢复。
示例:
#A file with values:
66 6F 6F 00 62 61 72
#Displays:
foo
#When it should read something like:
fooNULLbar
在 Eclipse 中运行程序时,如果它没有扩展到 I/O,那么这只不过是一个小烦恼。如果我尝试使用 InputStream 或 Reader 读取上一个示例中的文件,则会导致文件在 null 之前结束。
示例:
# File: test
66 6F 6F 00 62 61 72
# File: Test.java
import java.io.File;
import java.io.FileInputStream;
public class Test {
public static void main( String[] args ) {
try {
File file = new File( "test" );
FileInputStream in = new FileInputStream( file );
int b;
while ( ( b = in.read() ) != -1 ) {
System.out.print( (char) b );
}
in.close();
} catch ( Exception e ) {
System.err.println( e.getMessage() );
}
}
}
# Produces output to Eclipse console:
foo
# While when run from command line, it produces:
fooNULLbar
当我在 Eclipse 外部打开这些文件或运行这些程序时,一切正常,包括在 Eclipse 中编译的程序。
我在谷歌搜索问题时遇到了这个问题,但我不确定是否是同一个问题: https://bugs.eclipse.org/bugs/show_bug.cgi?id=283231
预先感谢您的所有帮助。
I've run into a very strange problem in Eclipse, and I'm not sure if its a bug or if some settings are just wrong.
I'm using Fedora Eclipse on Fedora 14.
I first noticed this issue when opening a file with null characters in the eclipse text editor. After the first null character was reached, no further characters were displayed. Furthermore, if the file contained a new line after the null everything between the null and newline was not displayed, but resumed properly after the newline.
Example:
#A file with values:
66 6F 6F 00 62 61 72
#Displays:
foo
#When it should read something like:
fooNULLbar
This wouldn't be more than a minor annoyance if it didn't extend to I/O when running a program in Eclipse. If I try to read the file in the previous example with an InputStream or Reader, it results in an end of file just before the null.
Example:
# File: test
66 6F 6F 00 62 61 72
# File: Test.java
import java.io.File;
import java.io.FileInputStream;
public class Test {
public static void main( String[] args ) {
try {
File file = new File( "test" );
FileInputStream in = new FileInputStream( file );
int b;
while ( ( b = in.read() ) != -1 ) {
System.out.print( (char) b );
}
in.close();
} catch ( Exception e ) {
System.err.println( e.getMessage() );
}
}
}
# Produces output to Eclipse console:
foo
# While when run from command line, it produces:
fooNULLbar
When I open these files or run these programs outside of Eclipse, everything works correctly including programs compiled in Eclipse.
I came across this while Googling the issue, but I'm not sure if it's the same issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=283231
Thanks in advance for all help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您错误地解释了输出,即字符串在屏幕上的呈现方式。因为如果你使用一些奇怪的字体,任何正常的字符串都会看起来很奇怪。常规 unicode 字符串在一种语言环境中看起来会有所不同,但在另一种语言环境中则不然。即使是正确呈现的包含 Devnagari 字符的 unicode 字符串,对于以前从未见过这些字符的人来说也会显得很奇怪。因此空字符可能看起来很奇怪。这可能是因为它不是您首先想要显示的字符。另外,空字符不是字符串终止符(正如您可能认为的,由于 C/C++)。 Java 确实可以正确处理它。
作为一个简单的测试,只需尝试在 Java 程序中打印 '\0' 并查看得到什么输出,'
null
' 或其他奇怪的东西。 :)来自 Java 文档:
print
打印字符串。如果参数为 null,则打印字符串“
null
”。否则,字符串的字符将根据平台的默认字符编码转换为字节,并且这些字节的写入方式与 write(int) 方法完全相同。参数:
希望这有帮助。
I think you are interpreting the output incorrectly, i.e. how the string is rendered on screen. Because any normal string will look strange if you use some strange font. A regular unicode string will look something different in one locale but not the other. Even a properly rendered unicode string containing, for e.g. Devnagari characters, will look strange to someone who has never seen those before. Hence the null character probably will look strange. This may be because it is not a character that you want to display in the first place. Also, null character is not the string terminator (as you might be thinking due to C/C++). Java does handle it properly.
As a simple test, just try to print '\0' in the Java program and see what output you get, '
null
' or something strange. :)From the Java Doc:
print
Print a string. If the argument is null then the string "
null
" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of thewrite(int)
method.Parameters:
Hope this helps.