跟踪我的项目扫描程序,这是一个扫描* .txt文件目录的java工具。
谢谢大家的帮助!但是现在我有一个不同的问题。
当我得到结果时,它会读取我不希望它读取的字符串。如何阻止这些字符串我不想读它?
这就是我的意思:
Enter item to find: 1112
Found 125000000
[BANK]
[FRIENDS]
[IGNORES]
[EOF]
time(s) in kyle.txt!
Found 1000000
[FRIENDS]
[IGNORES]
[EOF]
time(s) in kylea.txt!
Press any key to continue . . .
我希望它显示如下结果:
Enter item to find: 1112
Found 125000000 time(s) in kyle.txt!
Found 1000000 time(s) in kylea.txt!
Press any key to continue . . .
我该怎么做?我尝试创建一个数组列表来阻止我不需要的其他结果,但我也无法让它工作。
感谢您的帮助!
此外,这是我的代码:
import java.io.*;
import java.util.*;
public class ItemScanner {
public static void main(String args[]) {
System.out.print("Enter item to find: ");
Scanner sc = new Scanner(System.in);
find(sc.nextLine());
}
public static void find(String delim) {
File dir = new File("accounts");
if (dir.exists()) {
String read;
try {
File files[] = dir.listFiles();
for (int i = 0; i < files.length; i++) {
File loaded = files[i];
if (loaded.getName().endsWith(".txt")) {
BufferedReader in = new BufferedReader(new FileReader(loaded));
StringBuffer load = new StringBuffer();
while ((read = in.readLine()) != null) {
load.append(read + "\n");
}
String delimiter[] = new String(load).split(delim);
if(delimiter.length > 1) {
System.out.println("Found " + (delimiter[1]) + " time(s) in " + loaded.getName() + "!");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("error: dir wasn't found!");
}
}
}
此外,它扫描的帐户示例如下:
[ACCOUNT]
character-username = kylea
character-password =
[CHARACTER]
character-height = 0
character-isactive = 1
character-messages = 0
character-lastconnection =
character-lastlogin = 2009/11/27
character-energy = 100
[EQUIPMENT]
character-equip = 0 4724 0
character-equip = 1 1052 0
character-equip = 2 6585 0
character-equip = 3 4151 0
character-equip = 4 4720 0
character-equip = 5 1215 0
character-equip = 6 -1 0
character-equip = 7 4722 0
character-equip = 8 -1 0
character-equip = 9 775 0
character-equip = 10 1837 0
character-equip = 11 -1 0
character-equip = 12 6735 0
character-equip = 13 -1 0
[LOOK]
character-look = 0 1
[SKILLS]
character-skill = 0 18 445633
character-skill = 1 15 440000
character-skill = 2 15 440000
character-skill = 3 199 1402300000
character-skill = 4 22 0
character-skill = 5 1 0
character-skill = 6 1 0
character-skill = 7 1 0
character-skill = 8 1 0
character-skill = 9 1 0
character-skill = 10 1 0
character-skill = 11 1 0
character-skill = 12 1 0
character-skill = 13 1 0
character-skill = 14 1 0
character-skill = 15 1 0
character-skill = 16 1 0
character-skill = 17 1 0
character-skill = 18 1 0
character-skill = 19 1 0
character-skill = 20 1 0
character-skill = 21 1 0
character-skill = 22 1 0
character-skill = 23 1 0
character-skill = 24 1 0
[ITEMS]
character-item = 0 6570 11
character-item = 1 666570 0525
character-item = 2 1165701 55
character-item = 3 55 66
character-item = 4 963 51
character-item = 5 961 55
[BANK]
character-bank = 0 996 1000000
[FRIENDS]
[IGNORES]
[EOF]
答案 0 :(得分:1)
您在此代码中正在执行的操作是读取字符串缓冲区中的.txt文件的内容,并在示例“1112”中将该文件内容拆分到分隔符上,然后打印该文件的其余部分。
所以,如果你的文件包含“aaaa11121112uh哦”它会打印“1112uh哦”?我不清楚你想要完成什么。
只显示数字的快速修复将使用:
Long.valueOf(delimiter[1])
仅解析剩余部分的数字部分并打印出来。我觉得这不会解决潜在的问题,所以也许你可以详细说明你想在这里实现的目标?
修改强>
用一个数据文件的例子更新你的问题后,(看起来非常像.ini文件:-))我认为通过阅读Properties class的javadoc,特别是{{{ 3}}和load()方法,将您的角色数据存储在Properties
集合中。
答案 1 :(得分:0)
您似乎混淆了一些示例代码。您似乎从问题和main()中寻找特定项目,但在代码中,您使用传入参数作为分隔符来分割您读取的行,并计算单词数。
要执行您的问题所要求的问题,您需要使用逻辑来拆分读入行,然后迭代它,与要查找的项目进行比较。