Java 搜索、修改和删除文本文件
我正在尝试在文本文件中搜索、编辑和/或删除特定单词的方法Java 搜索、修改和删除文本文件,java,text,replace,text-files,Java,Text,Replace,Text Files,我正在尝试在文本文件中搜索、编辑和/或删除特定单词的方法 private void modifyShow() throws FileNotFoundException, IOException { Scanner input = new Scanner(System.in); System.out.println("Please search for a TV Show\nExample: Simpsons"); String tvSearch = inp
private void modifyShow() throws FileNotFoundException, IOException {
Scanner input = new Scanner(System.in);
System.out.println("Please search for a TV Show\nExample: Simpsons");
String tvSearch = input.nextLine();
System.out.println("Displaying results for: " + tvSearch);
String searchTerm = tvSearch;
searchTerm = searchTerm.toLowerCase();
int count = 0;
Scanner show = new Scanner(new File("src/TVShows.txt"));
while (show.hasNext()) {// loop
if (show.equals(searchTerm)) { // find
System.out.println(show);// display
}
}
}
所以它实际上并没有搜索文件,但它会打开它,我相信,但是while语句会将它抛出。所以一旦它找到了需要打印的单词
System.out.println("Enter a new name for " + tvSearch + ".");
将if show.equalsearchterm替换为if show.next.equalsearchterm,搜索将起作用。实际上,您正在将扫描仪对象与字符串进行比较。但它仍然不会显示搜索词。您知道为什么吗?