请问一下下面代码怎么翻译成 Java ?java 中有没有 feof() 函数#include <stdio.h>int main() {
int i;
char* filename = "1.txt";
char s[100]; FILE* fp = fopen(filename, "r"); if (fp != NULL) { while(!feof(fp)) {
fscanf(fp, "%s %d \n", s, &i);
printf("%s %d ", s, i);
if (!strcmp(s, "Long")) {
printf("long\n");
}
if (!strcmp(s, "Int")) {
printf("int\n");
}
}
} fclose(fp);
return 0;
}// 1.txt
Int -1166707595
Long 2594814172
Long 2594820957
Int 0
Long 86771
Int 24
Int 0
Long 0
Long 0
Int 0
Int 0
Int 0
Long 6804000
Long 0
Long 0
Long 0
Int 0
Int 0
Int 0
Long 0
int i;
char* filename = "1.txt";
char s[100]; FILE* fp = fopen(filename, "r"); if (fp != NULL) { while(!feof(fp)) {
fscanf(fp, "%s %d \n", s, &i);
printf("%s %d ", s, i);
if (!strcmp(s, "Long")) {
printf("long\n");
}
if (!strcmp(s, "Int")) {
printf("int\n");
}
}
} fclose(fp);
return 0;
}// 1.txt
Int -1166707595
Long 2594814172
Long 2594820957
Int 0
Long 86771
Int 24
Int 0
Long 0
Long 0
Int 0
Int 0
Int 0
Long 6804000
Long 0
Long 0
Long 0
Int 0
Int 0
Int 0
Long 0
public static void main(String[] args) {
File file = new File("1.txt");
Reader reader = null;
String strInt = "Int";
try {
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
//String str1 = new String (tempchar);
//if (str1.equals(strInt)) {
System.out.print((char) tempchar);
//}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
這邊 java code 怎麼加入 if 判斷 Int or Long?謝謝
read() 读出来的都只是整型,估计JAVA解决不了这个事情,在文件中的二进制,可读为字符,也可是整型,也可是长整型。因C多年未接触,上面的代码应该是数值前有一个字符标识:int 或 long。
那么读到int字符后面的数字是整型,读到long后面的长整型,不知道理解是否正确。