Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 同时读取两个文件_Java_File - Fatal编程技术网

Java 同时读取两个文件

Java 同时读取两个文件,java,file,Java,File,假设我们有两个文件,分别为f1和f2。 还假设有一个名为comparison(文件f1,文件f2)的函数。 此函数将获取两个文件作为参数,并从f1中获取第一个字符(单词),并将其与f2中的所有字符进行比较,直到结束,然后选择第二个字符并将其作为第一个字符执行,以此类推 我的问题是:我如何实现这一点?我需要知道EOF吗?如果是的话,如何得到它 假设文件是纯文本(.txt),并且每个单词都在一行中。 例如: f1: I am new to java f2: java is a progr

假设我们有两个文件,分别为
f1和f2。

还假设有一个名为
comparison(文件f1,文件f2)
的函数。 此函数将获取两个文件作为参数,并从f1中获取第一个字符(单词),并将其与f2中的所有字符进行比较,直到结束,然后选择第二个字符并将其作为第一个字符执行,以此类推

我的问题是:我如何实现这一点?我需要知道EOF吗?如果是的话,如何得到它

假设文件是纯文本
(.txt)
,并且每个单词都在一行中。 例如:

f1:   
I
am
new
to
java

f2:

java 
is
a
programing
language
代码如下:

   static void comparision(File f, File g) throws Exception
    {



       Set<String> text = new LinkedHashSet<String>();
BufferedReader br = new BufferedReader(new FileReader(g));
for(String line;(line = br.readLine()) != null;)
   text.add(line.trim().toString());
        if(text==null)
            return;


        BufferedReader br = new BufferedReader(new FileReader(f));
        String keyword = br.readLine();

        if (keyword != null) {

            Pattern p = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE);
            StringBuffer test = new StringBuffer(text.toString());
            matcher = p.matcher(test);
            if (!matcher.hitEnd()) {
                 total++;
                 if (matcher.find()) {
                     //do sth           
                 }
             }
         }
    }
静态无效比较(文件f、文件g)引发异常
{
Set text=new LinkedHashSet();
BufferedReader br=新的BufferedReader(新文件读取器(g));
for(字符串行;(line=br.readLine())!=null;)
text.add(line.trim().toString());
if(text==null)
返回;
BufferedReader br=新的BufferedReader(新文件读取器(f));
字符串关键字=br.readLine();
if(关键字!=null){
Pattern p=Pattern.compile(关键字,Pattern.CASE\u不区分大小写);
StringBuffer测试=新的StringBuffer(text.toString());
匹配器=p.matcher(测试);
如果(!matcher.hitEnd()){
总计++;
if(matcher.find()){
//做某事
}
}
}
}
由jcolebrand编辑

需要考虑的是,我们需要这样的程序流(伪代码)

函数(file1、file2)抛出异常{
ArrayList list1,list2;//有人说我们应该使用ArrayList;-)
字符串readinTempValue=null;
br=BufferedReader(文件1)//我们已经在使用BufferedReader了
readinTempValue=br.ReadLine();
//这是一个循环结构
而(readinTempValue!=null){//请相信我
//我们需要将字符串放入数组列表。。。。
//我们如何向列表1添加值
readinTempValue=br.ReadLine();//相信我
}
br=BufferedReader(文件2)//我们已经在使用BufferedReader了
readinTempValue=br.ReadLine();
//这是一个循环结构
而(readinTempValue!=null){//请相信我
//我们需要将字符串放入数组列表。。。。
//我们如何将该值添加到列表2中
readinTempValue=br.ReadLine();//相信我
}
foreach(列表1中的值){
foreach(列表2中的值){
将列表1中的值与列表2中的值进行比较
}
}
}
简单的基本算法(可以根据您想要比较的原因进行微调)

对OP代码的修改

static int comparision(File f, File g) throws Exception
    {
        int occurences = -1;

        Set<String> text = new HashSet<String>();

        BufferedReader br = new BufferedReader(new FileReader(g));
        String line = br.readLine();

        while (line != null)
        {
            String trimmedLine = line.trim();
            if (trimmedLine.length() > 0)
            {
                text.add(trimmedLine.toString());
            }
            line = br.readLine();
        }

        if (text.isEmpty())
        {
            // file 1 doesn't contain any useful data
            return -1;
        }

        br = new BufferedReader(new FileReader(f));
        String keyword = br.readLine();

        if (keyword != null)
        {
            String trimmedKeyword = keyword.trim();
            if (trimmedKeyword.length() > 0)
            {
                if (text.contains(trimmedKeyword))
                {
                    occurences++;
                }
            }
            line = br.readLine();
        }
        return occurences;
    }
静态整数比较(文件f、文件g)引发异常
{
int发生率=-1;
Set text=new HashSet();
BufferedReader br=新的BufferedReader(新文件读取器(g));
String line=br.readLine();
while(行!=null)
{
String trimmedLine=line.trim();
如果(trimmedLine.length()>0)
{
text.add(trimmedLine.toString());
}
line=br.readLine();
}
if(text.isEmpty())
{
//文件1不包含任何有用的数据
返回-1;
}
br=新的BufferedReader(新的文件读取器(f));
字符串关键字=br.readLine();
if(关键字!=null)
{
字符串trimmedKeyword=关键字.trim();
如果(trimmedKeyword.length()>0)
{
if(text.contains(trimmedKeyword))
{
事件++;
}
}
line=br.readLine();
}
返回事件;
}

对于其他人来说,这似乎是一个很有启发性的问题。。。但它需要澄清。刚刚发布了一些编辑。。。您能添加预期的输出吗?@user1064929您目前做得很好。但您需要将其分为两个任务:在中读取两个文件,将文件1中的每个单词与文件2中的每个单词进行比较。看起来你把这两个文件混在一起了。@jcolebrand好吧,我的问题是如何在第二个文件上迭代,直到它结束?@user1064929如果你能首先将每个文件读入列表类型结构,那么你可以使用迭代器遍历列表结构中的每个元素。最简单的形式是数组,并使用索引语法,如
foreach(var i=0;i
(但请注意,这不是好代码,这是看起来像真实代码的伪代码),您已经在使用bufferedreader和用于读入的字符串段读入了。您需要的是一个临时位置来存放您正在阅读的内容。您应该解释如何创建哈希集。他已经不知道怎么做了。虽然我知道哈希集是什么,但OP不会。我知道如何创建哈希设置。但是如果您不介意,请查看我的编辑,然后告诉我如何更改它以获得answer@user1064929我已经更新了我的帖子,修改了你的代码。你不需要PatternMatcher。此外,代码没有遵循良好的实践,但是您得到了理想答案。啊,如果没有逻辑流,这可能会更复杂。我觉得很好@user1064929您在这里所做的是使用两个循环和哈希集的性质来跳过我的第二个循环,因此这是使用我的第一个循环和第三个循环,因为哈希集的性质。
Read the second file and create a HashSet "hs"
for each word "w" in file 1
  if(hs.contains(w))
  {
    w is present in the second file
  }
  else
  {
    w is not present in the second file
  }
static int comparision(File f, File g) throws Exception
    {
        int occurences = -1;

        Set<String> text = new HashSet<String>();

        BufferedReader br = new BufferedReader(new FileReader(g));
        String line = br.readLine();

        while (line != null)
        {
            String trimmedLine = line.trim();
            if (trimmedLine.length() > 0)
            {
                text.add(trimmedLine.toString());
            }
            line = br.readLine();
        }

        if (text.isEmpty())
        {
            // file 1 doesn't contain any useful data
            return -1;
        }

        br = new BufferedReader(new FileReader(f));
        String keyword = br.readLine();

        if (keyword != null)
        {
            String trimmedKeyword = keyword.trim();
            if (trimmedKeyword.length() > 0)
            {
                if (text.contains(trimmedKeyword))
                {
                    occurences++;
                }
            }
            line = br.readLine();
        }
        return occurences;
    }