c to java 文件处理

shihyuyao 2013-03-26 10:19:26
请问一下下面代码怎么翻译成 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
...全文
93 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
shihyuyao 2013-03-27
  • 打赏
  • 举报
回复
import java.io.*;

public class test {
    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? 謝謝
shihyuyao 2013-03-27
  • 打赏
  • 举报
回复
那請問在 java 中 如何讀取文件中每行 Int -1166707595 Int 是String type , -1166707595 是整數類型
STEEL-CHINA 2013-03-27
  • 打赏
  • 举报
回复
引用 1 楼 shihyuyao 的回复:
C/C++ code?12345678910111213141516171819202122import java.io.*; public class test { public static void main(String[] args) { File file = new File("1.txt"); Reader reader ……
read() 读出来的都只是整型,估计JAVA解决不了这个事情,在文件中的二进制,可读为字符,也可是整型,也可是长整型。 因C多年未接触,上面的代码应该是数值前有一个字符标识:int 或 long。 那么读到int字符后面的数字是整型,读到long后面的长整型,不知道理解是否正确。
liujun3512159 2013-03-27
  • 打赏
  • 举报
回复
这么复杂,看不懂c

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧