【发布时间】:2015-11-22 03:49:38
【问题描述】:
我有以下代码,我在其中计算文件中的行数。而且我还想检索最后一行(整数):
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ( (readChars = is.read(c)) != -1) {
for (int i = 0; i < readChars; ++i){
if (c[i] == '\n'){
++count;
empty = true;
lastLine = c[i].intValue();
} else {
empty = false;
}
}
}
if (!empty) {
count++;
}
System.out.println("the last line was " + lastLine);
return count;
我添加了这一行 - lastLine = c[i].intValue();
但这给出了错误:
C:\Java_Scratch>javac ParentClass2.java ParentClass2.java:90: 字节 不能取消引用 lastLine = c[i].intValue();
如何将字节转换为 int ?
【问题讨论】:
-
byte是一个原语,它没有可以在其上执行的“方法”。也许试试(int)c[i]; -
@MadProgrammer - 好的,知道了 - 谢谢!
-
@MadProgrammer - 奇怪的事情 - 这确实编译,但即使最后一行是
27它告诉我它是10 -
which is integer 在二进制中并不真正意味着任何东西。您必须以某种方式解释这些字节才能在整数上下文中理解它们。
-
if (c[i] == '\n'){ .. lastLine = (int)c[i];},是什么让您认为lastLine将不同于\n的int表示形式?
标签: java