繁体   English   中英

在Java中的文本文件中查找特定值

[英]Finding specific value in a text file in java

我目前正在使用Java练习文件处理,因此我尝试创建一种方法,该方法将使用writeUTF()和其他写入函数对文件中的用户输入进行编码。

我的代码如下所示:

public static void writeInfo(File file, int id, String name, int age) throws FileNotFoundException{
    DataOutputStream dataOut = new DataOutputStream(new
            BufferedOutputStream(new FileOutputStream(file, true)));
    try{
        dataOut.writeInt(id);
        dataOut.writeUTF(name);
        dataOut.writeInt(age);
        dataOut.close();
    }catch(FileNotFoundException ex){
        System.err.println("File not found !");
    }catch(IOException ex){
        System.err.println("Error writing in file !");
    }finally{
        try{
            dataOut.close();
        }catch(IOException ex){
            System.err.println(ex);
        }
    } 
}

现在我的问题是,在有多个输入之后,我无法打印出一组特定的值。 例如,如果我有3组输入:

ID-3名称-Ace年龄-20

ID-8名-玛丽年龄-22

ID-5名字-Karl Age-25

如果我想找到ID值为5的一组输入,则输出应为:

ID - 5
Name - Karl
Age - 25

但是我总是在运行后得到EndOfFileException。

这是我如何找到特定值的代码:

 public static void readID(File file, int id) throws FileNotFoundException{
    DataInputStream dataIn = new DataInputStream(new
        BufferedInputStream(new FileInputStream(file)));
    try{
        while(dataIn.available()>0){
            if(dataIn.readInt() != id){
                dataIn.read();
                continue;
            }else{
                System.out.println("ID : " + dataIn.readInt());
                System.out.println("Name : " + dataIn.readUTF());
                System.out.println("Age : " + dataIn.readInt());
                System.out.println("\n");
            }
        }
    }catch(FileNotFoundException e){
        System.err.println("File not found !");
    }catch(IOException e){
        System.err.println(e);
    }finally{
        try{
            dataIn.close();
        }catch(IOException ex){
            System.err.println(ex);
        }
    }

我还尝试了其他方法,例如将我首先读取的值包含到变量中。 我知道我做错了事,仍然在网上寻找解决方案。 但我希望你们能帮助我,以便我仍然可以学习更多。

这是问题所在:

    while(dataIn.available()>0){
        if(dataIn.readInt() != id){ // <--- #1
            dataIn.read(); // <--- #2
            continue;
        }else{
            System.out.println("ID : " + dataIn.readInt()); // <-- #3
            System.out.println("Name : " + dataIn.readUTF());
            System.out.println("Age : " + dataIn.readInt());
            System.out.println("\n");
        }
    }

第1行将在流中使用一个int ,如果该int是您想要的(如果statement为false ),则执行else块,但是这里的第3行中的readInt将在最后一个readInt (应为name字段)之后连续读取。

此外,如果if语句为true,则第2行将读取一个byte并跳过它,这对于name字段和age字段是不够的。

因此,以下代码可能有效:

while(dataIn.available()>0){
    int aId = dataIn.readInt();
    if(aId != id){
        dataIn.readUTF(); // skip name
        dataIn.readInt(); // skip age
        continue;
    }else{
        System.out.println("ID : " + aId);
        System.out.println("Name : " + dataIn.readUTF());
        System.out.println("Age : " + dataIn.readInt());
        System.out.println("\n");
    }
}

或更简而言之:

while(dataIn.available()>0){
    int aId = dataIn.readInt();
    String name = dataIn.readUTF();
    int age = dataIn.readInt();

    if (aId == id) {
        System.out.println("ID : " + aId);
        System.out.println("Name : " + name);
        System.out.println("Age : " + age);
        System.out.println("\n");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM