Java:搜索每行中字符串出现的文件

时间:2015-04-27 18:12:05

标签: java string search

我想使用每行中的第一个字符串搜索整个文本文件。我想取第一个String并查找它在文件中出现的次数,然后在该行前写下该数字。

如何在每行中搜索该文件中的第一个字符串?我怎么说我想找到它的第二次/第三次出现?最后,我如何为文件中的每一行执行此操作?

以下是它的工作方式:

如果第一个String是第一个出现的,那么在该行之前写一个1。

对于该特定String的第二次出现,在该行之前写一个2。

对于该String的第三次出现,在该行之前写一个3。

这就是我的想法(尽管我对一个新想法完全开放):

   BufferedReader br = new BufferedReader(new FileReader( "myfile" ));

   String line;
   String firstString;

        while ((line= br.readLine() ) != null)
          { String arr[] = line.split( " ");
            firstString = arr[0];

          //if firstString only occurs once
          bw.write ("1" + line);

          //if firstString occurs twice
          bw.write ("2" + line);

          //if firstString occurs three times
          bw.write ("3" + line);

小样本输入:

 Cater Megan 12354134 employee23411
 White Regan 54321123 employee90843
 White Haley 09834809 employee09842
 Rohn Smith 98234789 employee23848
 White Devon 0983489 employee75401
 Cater John 09883548 employee09834

示例输出:

 1Cater Megan 12354134 employee23411
 1White Regan 54321123 employee90843
 2White Haley 09834809 employee09842
 1Rohn Smith 98234789 employee23848
 3White Devon 0983489 employee75401
 2Cater John 09883548 employee09834

2 个答案:

答案 0 :(得分:0)

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Set;

public class stringval {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    String string=new String("Hi");     
    String input=null;
    HashMap<String, Integer> countlines=new LinkedHashMap();//<String, Integer>();
    HashMap<String, Integer> checkfirstname=new HashMap<>();
    BufferedReader br=null;
    try {
        br = new BufferedReader(new FileReader("C:/test.txt"));
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        while ((input= br.readLine() ) != null){
            String[] firstname=input.split(" ");
            if(checkfirstname.containsKey(firstname[0])==true){
                Integer i=checkfirstname.get(firstname[0]);
                countlines.put(input, i+1);             
                checkfirstname.put(firstname[0], i+1);
            }
            else{
                countlines.put(input,1);
                checkfirstname.put(firstname[0],1);
            }


        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Set<String> keys=countlines.keySet();
    for(String s:keys)
        System.out.println(countlines.get(s)+s);
}

}

Output:
1Cater Megan 12354134 employee23411
1White Regan 54321123 employee90842
2White Haley 09834809 employee90842
1Rohn Smith 98234789 employee23848
3White Devon 0983489 employee75401
2Cater John 09883548 employee09834

答案 1 :(得分:0)

一次读取所有行,然后你可以遍历它们并进行必要的检查,以便知道一行的第一个字符串出现多少次作为另一行的第一个字符串以保持计数。

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class yourClass {
    public static void main(String[] args) throws Exception {
        List<String> sampleInput = Files.readAllLines(Paths.get(yourSampleInput));
        for (int i = 0; i < sampleInput.size(); i++) {
            String input = sampleInput.get(i);

            // This line has already been tagged with a count, so just skip it
            if (Character.isDigit(input.charAt(0))) {
                continue;
            }

            // You already know this is the first occurrence
            int count = 1;
            sampleInput.set(i, count + " " + input);
            String[] inputPieces = input.split(" ");

            // Check remaining lines after the line you are on to see if there is a match
            for (int j = i + 1; j < sampleInput.size(); j++) {
                if (sampleInput.get(j).startsWith(inputPieces[0])) {
                    // This is another occurence of the line that you are processing from the outer for loop
                    count++;
                    sampleInput.set(j, count + " " + input);
                }
            }
        }

        for (String output : sampleInput) {
            System.out.println(output);
        }
    }
}

结果:

enter image description here