[英]bufferedreader search text file?
file txt:文件.txt:
1 aaa 100 1 aaa 100
where insert number 1 the output null其中插入数字 1 输出为空
java :爪哇:
private static void SearchForId() {
try {
Scanner scanner = new Scanner(System.in);
System.out.println("Inter Id");
String id = scanner.next();
BufferedReader Buffer = new BufferedReader(new FileReader(new File(
"D://Save.txt")));
String line = null;
while ((line = Buffer.readLine()) != null) {
if (line.trim().equals(id)) {
System.out.println(line + "\n");
}
}
} catch (Exception e) {
System.out.print("" + e);
}
}
where the problem.??问题出在哪里。?
You can try this way.你可以试试这个方法。
private static void SearchForId() {
try {
Scanner scanner = new Scanner(System.in);
System.out.println("Inter Id");
String id = scanner.next();
BufferedReader Buffer = new BufferedReader(new FileReader(new File(
"D://Save.txt")));
String line = null;
while ((line = Buffer.readLine()) != null) {
String arr[] = line.split(" ");
for (int i = 0; i < arr.length; i++) {
if (arr[i].trim().equals(id)) {
System.out.println(line);
}
}
}
} catch (Exception e) {
System.out.print("" + e);
}
If I understand the question correctly, you don't need Scanner
here:如果我正确理解了这个问题,那么您在这里不需要Scanner
:
private static void SearchForId() {
try {
System.out.println("Inter Id");
BufferedReader Buffer = new BufferedReader(new FileReader(new File(
"D://Save.txt")));
String[] line = Buffer.readLine().split(" ");
int a = Integer.parseInt(line[0]);
String s = line[1];
int b = Integer.parseInt(line[2]);
} catch (Exception e) {
System.out.print("" + e);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.