我正在尝试创建一个java程序,它可以读取名为file1.txt的文件并存储其字符串,并将这些字符串搜索到另一个名为file2.txt的文件中,如果未找到匹配,则打印file1.txt中的特定字符串。
public static void main(String[] args)
{
try
{
BufferedReader word_list = new BufferedReader(new FileReader("file1.txt"));
BufferedReader eng_dict = new BufferedReader(new FileReader("file2.txt"));
String spelling_word = word_list.readLine();
String eng_dict_word = eng_dict.readLine();
while (spelling_word != null)
{
System.out.println(spelling_word);
spelling_word = word_list.readLine();
if(eng_dict_word.contains(spelling_word))
{
System.out.println("Word found "+spelling_word);
}
else
{
System.out.println("Word not found "+spelling_word);
}
}
word_list.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}现在,我可以从file1.txt获取数据,但无法搜索file1的数据,例如,在file2.txt中搜索单词"Home“
在这里,File1.txt包含Home,File2.txt包含Home,因此应该打印Home

发布于 2019-04-09 22:15:23
首先,您需要读取第一个文件。最好是SET(),因为它会去掉重复的字符串。您将拥有set1
完成后,您需要读取第二个文件,并执行相同的操作。你会得到set2
现在,您需要在参数为set2的set1上使用RemoveAll()方法。set1中剩余的内容需要打印在屏幕上。你可以用lambda做到这一点。
请参阅THIS了解如何读取文件。
请看下面的代码:
Set<String> set1 = new HashSet<>();
Set<String> set2 = new HashSet<>();
try (FileReader reader = new FileReader("file1.txt");
BufferedReader br = new BufferedReader(reader)) {
// read line by line
String line;
while ((line = br.readLine()) != null) {
set1.add(line);
}
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
try (FileReader reader = new FileReader("file2.txt");
BufferedReader br = new BufferedReader(reader)) {
// read line by line
String line;
while ((line = br.readLine()) != null) {
set2.add(line);
}
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
set1.removeAll(set2);
set1.forEach(System.out::println);发布于 2019-04-09 23:21:10
使用Regex来满足特定需求,下面是针对您的问题的重构代码。如果这对你有效,请告诉我。
public static void main(String[] args) throws IOException
{
try
{
BufferedReader word_list = new BufferedReader(new FileReader("resources/file1.txt"));
BufferedReader eng_dict = new BufferedReader(new FileReader("resources/file2.txt"));
String spelling_word = word_list.readLine();
String eng_dict_word = eng_dict.readLine();
int matchFound = 0;
Matcher m = null;
while (spelling_word != null)
{
// creating the pattern for checking for the exact match on file2.txt
String spelling_word_pattern = "\\b" + spelling_word + "\\b";
Pattern p = Pattern.compile(spelling_word_pattern);
while(eng_dict_word !=null) {
m = p.matcher(eng_dict_word);
if(m.find()) {
matchFound = 1;
break;
}
eng_dict_word = eng_dict.readLine();
}
if(matchFound == 1) {
System.out.println("Word found " + m.group());
}else {
System.out.println("Word not found "+ spelling_word);
}
spelling_word = word_list.readLine();
eng_dict = new BufferedReader(new FileReader("resources/file2.txt"));
eng_dict_word = eng_dict.readLine();
matchFound = 0;
}
word_list.close();
eng_dict.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}file1.txt内容
Homeee
Ho
hello
hom
xx
Mefile2.txt内容
Home
Me
H
Homey结果
Word not found Homeee
Word not found Ho
Word not found hello
Word not found hom
Word not found xx
Word found Mehttps://stackoverflow.com/questions/55594544
复制相似问题