[英]Finding specific value in a text file in java
I'm currently practicing file handling in Java so I tried to create a method that will encode user inputs on a file using writeUTF()
and other write functions. 我目前正在使用Java练习文件处理,因此我尝试创建一种方法,该方法将使用writeUTF()
和其他写入函数对文件中的用户输入进行编码。
My code looks like this : 我的代码如下所示:
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);
}
}
}
Now my problem is that after having multiple inputs, I can't print out a specific set of values. 现在我的问题是,在有多个输入之后,我无法打印出一组特定的值。 For example, if I have 3 sets of input : 例如,如果我有3组输入:
ID - 3 Name - Ace Age - 20 ID-3名称-Ace年龄-20
ID - 8 Name - Marie Age - 22 ID-8名-玛丽年龄-22
ID - 5 Name - Karl Age - 25 ID-5名字-Karl Age-25
If I want to find the set of inputs which has an ID value of 5, the output should be : 如果我想找到ID值为5的一组输入,则输出应为:
ID - 5
Name - Karl
Age - 25
But I always get an EndOfFileException after running. 但是我总是在运行后得到EndOfFileException。
This is the code how I find a specific value : 这是我如何找到特定值的代码:
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);
}
}
I tried also different ways like containing the values I read first into a variable. 我还尝试了其他方法,例如将我首先读取的值包含到变量中。 I know that I'm doing something wrong and still finding up solutions over the net. 我知道我做错了事,仍然在网上寻找解决方案。 But I hope you guys will help me so I can still learn more. 但我希望你们能帮助我,以便我仍然可以学习更多。
Here is the problem: 这是问题所在:
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");
}
}
Line #1 will consume an int
in the stream, if the int
is what you want (if statement is false
), else
block is executed, but here readInt
in line #3 will continuously read after the last readInt
, which should be name
field. 第1行将在流中使用一个int
,如果该int
是您想要的(如果statement为false
),则执行else
块,但是这里的第3行中的readInt
将在最后一个readInt
(应为name
字段)之后连续读取。
What's more, if the if
statement is true, line #2 will read one byte
and skip it, which is not enough for name
field and age
field. 此外,如果if
语句为true,则第2行将读取一个byte
并跳过它,这对于name
字段和age
字段是不够的。
So the following code may work: 因此,以下代码可能有效:
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");
}
}
Or more concisely: 或更简而言之:
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.