Java阅读2个文本文件 [英] Java reading 2 text files

查看:76
本文介绍了Java阅读2个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些指导,Java的新手,我在弄清楚我应该编写这个程序的方法时遇到问题。

我有2个文本文件1,其中包含关键字,另一个是包含各种信息的大文本文件。我需要弄清楚如何在大型测试文件中查找关键字文本文件中的单词。例如关键字有(饼,冰,吐司)然后在大文本文件中查找这些单词。用户将选择文本文件和大文本文件。



我尝试过:



我尝试了一段时间循环,但仍然坚持如何将关键字与大文本文件进行比较,使用SAcanner方法我的while循环似乎找不到生成正确的单词所需的单词输出。我做了一个ArrayList()但是输出会将它全部列在一行中,这对我来说似乎没有用,因为大文本文件中的关键字是单行的。

I need some guidance, new to Java, I am having a problem figuring out which way I should code this program.
I have 2 text files 1 with keywords in it and the other is a large text file with all kinds of information. I need to figure out how to look in the large test file for words from the keyword text file. for example keywords has (pie, ice, toast) then look for those words in the large text file. the text file and the large text file will be selected by the user.

What I have tried:

I have tried a while loop but get stuck on how to compare the keyword to the large text file, Using SAcanner method my while loop doesn't seem to find the words it needs to produce the correct output. I did an ArrayList() but the output would list it all in one line and that didn't seem to work for me, because the keywords in the large text file are on single lines.

推荐答案

首先将所有关键词读入列表(参见列表(Java Platform SE 7) [ ^ ])。然后,您需要读取大文件的每一行,跟踪行号。然后扫描每一行中的任何关键字,并打印一些信息,显示哪些行号以及找到的关键字。
Start by reading all the keywords into a List (see List (Java Platform SE 7 )[^] ). You then need to read each line of the large file, keeping track of the line number. Then scan each line for any of the keywords and print some information showing what line number, and which keywords were found.


这篇关于Java阅读2个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆