I created this simple example which is used to read Linux uptime:
我创建了这个简单的示例,用于读取Linux运行时:
public String getMachineUptime() throws IOException {
String[] dic = readData().split(" ");
long s = (long) Array.get(dic, 1);
return calculateTime(s);
}
private String readData() throws IOException {
byte[] fileBytes;
File myFile = new File("/proc/uptime");
if (myFile.exists()) {
try {
fileBytes = Files.readAllBytes(myFile.toPath());
} catch (java.nio.file.AccessDeniedException e) {
return null;
}
if (fileBytes.length > 0) {
return new String(fileBytes);
}
}
return null;
}
private String calculateTime(long seconds) {
int day = (int) TimeUnit.SECONDS.toDays(seconds);
long hours = TimeUnit.SECONDS.toHours(seconds)
- TimeUnit.DAYS.toHours(day);
long minute = TimeUnit.SECONDS.toMinutes(seconds)
- TimeUnit.DAYS.toMinutes(day)
- TimeUnit.HOURS.toMinutes(hours);
long second = TimeUnit.SECONDS.toSeconds(seconds)
- TimeUnit.DAYS.toSeconds(day)
- TimeUnit.HOURS.toSeconds(hours)
- TimeUnit.MINUTES.toSeconds(minute);
return "Day " + day + " Hour " + hours + " Minute " + minute
+ " Seconds " + second;
}
When I run the code I get this exception:
当我运行代码时,我得到这个异常:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
. lang。ClassCastException:. lang。字符串不能被转换为java.lang.Long
Is there any other way to convert the result?
有没有其他方法来转换结果?
2 个解决方案
#1
10
I believe you have to replace
我认为你必须替换
long s = (long) Array.get(dic, 1);
with
与
long s = Long.valueOf((String) Array.get(dic, 1));
or even better:
或者更好的是:
long s = Long.valueOf(dic[1]);
The reason is that your array consists of String
object, and direct casting won't work.
原因是您的数组由String对象组成,而直接强制转换将不起作用。
#2
3
The problem appears to be in the following line:
问题似乎如下:
long s = (long) Array.get(dic, 1);
-
The get(Object array, int index) method of java.lang.reflect.Array returns an instance of Object, which cannot be directly cast to long.
java.lang. reflection的get(Object array, int index)方法。数组返回一个对象实例,该对象不能直接转换为long。
-
You can access the element of the array simply by dic[1] instead of Array.get(dic, 1)
您可以通过dic[1]而不是数组访问数组的元素。get(dic,1)
Replace with the following code:
替换为以下代码:
long s = Long.parseLong(dic[1]);
#1
10
I believe you have to replace
我认为你必须替换
long s = (long) Array.get(dic, 1);
with
与
long s = Long.valueOf((String) Array.get(dic, 1));
or even better:
或者更好的是:
long s = Long.valueOf(dic[1]);
The reason is that your array consists of String
object, and direct casting won't work.
原因是您的数组由String对象组成,而直接强制转换将不起作用。
#2
3
The problem appears to be in the following line:
问题似乎如下:
long s = (long) Array.get(dic, 1);
-
The get(Object array, int index) method of java.lang.reflect.Array returns an instance of Object, which cannot be directly cast to long.
java.lang. reflection的get(Object array, int index)方法。数组返回一个对象实例,该对象不能直接转换为long。
-
You can access the element of the array simply by dic[1] instead of Array.get(dic, 1)
您可以通过dic[1]而不是数组访问数组的元素。get(dic,1)
Replace with the following code:
替换为以下代码:
long s = Long.parseLong(dic[1]);