同时从两个文件中读取

时间:2011-12-15 03:33:14

标签: java file

假设我们有两个文件f1 and f2.

还假设有一个名为comparision(File f1,File 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           
                 }
             }
         }
    }

由jcolebrand编辑

要考虑的事情,我们需要看起来像这样的程序流(伪代码)

function(file1,file2) throws exceptions{
  ArrayList<string> list1, list2; //somebody said we should use an ArrayList ;-)
  string readinTempValue = null;      

  br = BufferedReader(file1) //we are already using a BufferredReader
  readinTempValue = br.ReadLine();

  //this is a loop structure
  while (readinTempValue != null){ //trust me on this one

    //we need to get the string into the array list....
    //how can we ADD the value to list1
    readinTempValue = br.ReadLine(); //trust me on this one
  }


  br = BufferedReader(file2) //we are already using a BufferredReader
  readinTempValue = br.ReadLine();

  //this is a loop structure
  while (readinTempValue != null){ //trust me on this one

    //we need to get the string into the array list....
    //how can we ADD the value to list2
    readinTempValue = br.ReadLine(); //trust me on this one
  }

  foreach(value in list1){
    foreach(value in list2){
      compare value from list 1 to value from list 2
    }
  }
}

1 个答案:

答案 0 :(得分:1)

简单的基本算法(可根据您想要比较的原因进行微调)

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
  }

对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;
    }