Java 如何获取找到单词的行号? 私有静态列表计算(路径文本文件、字符串字){ 列表结果=新建ArrayList(); 试一试{ Files.lines(textFile).forEach(line->{ BreakIterator it=BreakIterator.getWordInstance(); it.setText(line.toString()); int start=it.first(); int end=it.next(); while(end!=BreakIterator.DONE){ 字符串currentWord=line.toString().substring(开始、结束); if(Character.isleterordigit(currentWord.charAt(0))){ if(当前字等于(字)){ 添加(新单词位置(文本文件,行)); 打破 } } 开始=结束; end=it.next(); } }); }捕获(IOE异常){ e、 printStackTrace(); } 返回结果; }

Java 如何获取找到单词的行号? 私有静态列表计算(路径文本文件、字符串字){ 列表结果=新建ArrayList(); 试一试{ Files.lines(textFile).forEach(line->{ BreakIterator it=BreakIterator.getWordInstance(); it.setText(line.toString()); int start=it.first(); int end=it.next(); while(end!=BreakIterator.DONE){ 字符串currentWord=line.toString().substring(开始、结束); if(Character.isleterordigit(currentWord.charAt(0))){ if(当前字等于(字)){ 添加(新单词位置(文本文件,行)); 打破 } } 开始=结束; end=it.next(); } }); }捕获(IOE异常){ e、 printStackTrace(); } 返回结果; },java,java-stream,Java,Java Stream,如何获取找到单词的行号? 我想在Lamda中使用流进行计算,但感到困惑。 你知道吗 public class Try { public static void main(String[] args) { Path path = Paths.get("etc/demo.txt"); List<String> result = compute(path, "Test"); result.stream().forEach(s -&

如何获取找到单词的行号? 我想在Lamda中使用流进行计算,但感到困惑。 你知道吗

public class Try {

    public static void main(String[] args) {

        Path path = Paths.get("etc/demo.txt");
        List<String> result = compute(path, "Test");
        result.stream().forEach(s -> System.out.println(s));
    }

    private static List<String> compute(Path textFilePath, String wordToFind) {

        List<String> results = new ArrayList<String>();
        // Added position and initialized with 0
        int[] position = new int[]{0};
        try {
            Files.lines(textFilePath).forEach(line -> {

                BreakIterator it = BreakIterator.getWordInstance();
                it.setText(line.toString());

                int start = it.first();
                int end = it.next();
                // Increment position by 1 for each line
                position[0] += 1;

                while (end != BreakIterator.DONE) {
                    String currentWord = line.toString().substring(start, end);
                    if (Character.isLetterOrDigit(currentWord.charAt(0))) {
                        if (currentWord.equals(wordToFind)) {
                            results.add("File Path: " + textFilePath + ", Found Word: " + wordToFind + ", Line: " + position[0]);
                            break;
                        }
                    }
                    start = end;
                    end = it.next();
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
        return results;
    }

}
输出:

Stream1
Review
Stream
2020-10-10 10:00
Test
0.0
admin HOST Test
Stream2
Review

注意:

Stream1
Review
Stream
2020-10-10 10:00
Test
0.0
admin HOST Test
Stream2
Review
  • 这是一个供您参考的示例,因为它使用了
    列表
  • 添加了
    int[]位置=新的int[]{0}
    位置[0]+=1用于显示行号
  • 在上述示例中,
    Test
    存在于行号
    5
    7
  • 输出:

    Stream1
    Review
    Stream
    2020-10-10 10:00
    Test
    0.0
    admin HOST Test
    Stream2
    Review
    

    注意:

    Stream1
    Review
    Stream
    2020-10-10 10:00
    Test
    0.0
    admin HOST Test
    Stream2
    Review
    
  • 这是一个供您参考的示例,因为它使用了
    列表
  • 添加了
    int[]位置=新的int[]{0}
    位置[0]+=1用于显示行号
  • 在上述示例中,
    Test
    存在于行号
    5
    7

  • 您可以使用
    LineNumberReader
    获取行号。看起来是这样的:

    private static List<A> compute(Path textFile, String word) {
        List<A> results = new ArrayList<A>();
    
        try (final LineNumberReader reader = new LineNumberReader(new FileReader(textFile.toFile()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                BreakIterator it = BreakIterator.getWordInstance();
                it.setText(line);
    
                int start = it.first();
                int end = it.next();
    
                final int lineNumber = reader.getLineNumber(); // here is your linenumber
    
                while (end != BreakIterator.DONE) {
                    String currentWord = line.substring(start, end);
                    if (Character.isLetterOrDigit(currentWord.charAt(0))) {
                        if (currentWord.equals(word)) {
                            results.add(new WordLocation(textFile, line));
                            break;
                        }
                    }
                    start = end;
                    end = it.next();
                }
            }
    
        } catch (IOException e) {
            e.printStackTrace();
        }
        return results;
    }
    
    私有静态列表计算(路径文本文件、字符串字){
    列表结果=新建ArrayList();
    try(final LineNumberReader reader=newlinenumberreader(newfilereader(textFile.toFile())){
    弦线;
    而((line=reader.readLine())!=null){
    BreakIterator it=BreakIterator.getWordInstance();
    it.setText(行);
    int start=it.first();
    int end=it.next();
    final int lineNumber=reader.getLineNumber();//这是您的行号
    while(end!=BreakIterator.DONE){
    字符串currentWord=行.子字符串(开始,结束);
    if(Character.isleterordigit(currentWord.charAt(0))){
    if(当前字等于(字)){
    添加(新单词位置(文本文件,行));
    打破
    }
    }
    开始=结束;
    end=it.next();
    }
    }
    }捕获(IOE异常){
    e、 printStackTrace();
    }
    返回结果;
    }
    
    您可以使用
    LineNumberReader
    获取行号。看起来是这样的:

    private static List<A> compute(Path textFile, String word) {
        List<A> results = new ArrayList<A>();
    
        try (final LineNumberReader reader = new LineNumberReader(new FileReader(textFile.toFile()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                BreakIterator it = BreakIterator.getWordInstance();
                it.setText(line);
    
                int start = it.first();
                int end = it.next();
    
                final int lineNumber = reader.getLineNumber(); // here is your linenumber
    
                while (end != BreakIterator.DONE) {
                    String currentWord = line.substring(start, end);
                    if (Character.isLetterOrDigit(currentWord.charAt(0))) {
                        if (currentWord.equals(word)) {
                            results.add(new WordLocation(textFile, line));
                            break;
                        }
                    }
                    start = end;
                    end = it.next();
                }
            }
    
        } catch (IOException e) {
            e.printStackTrace();
        }
        return results;
    }
    
    私有静态列表计算(路径文本文件、字符串字){
    列表结果=新建ArrayList();
    try(final LineNumberReader reader=newlinenumberreader(newfilereader(textFile.toFile())){
    弦线;
    而((line=reader.readLine())!=null){
    BreakIterator it=BreakIterator.getWordInstance();
    it.setText(行);
    int start=it.first();
    int end=it.next();
    final int lineNumber=reader.getLineNumber();//这是您的行号
    while(end!=BreakIterator.DONE){
    字符串currentWord=行.子字符串(开始,结束);
    if(Character.isleterordigit(currentWord.charAt(0))){
    if(当前字等于(字)){
    添加(新单词位置(文本文件,行));
    打破
    }
    }
    开始=结束;
    end=it.next();
    }
    }
    }捕获(IOE异常){
    e、 printStackTrace();
    }
    返回结果;
    }
    
    谢谢,很高兴我能帮忙。虽然还有很多其他的方法,但其中一种方法需要对现有代码进行最小的更改。谢谢,很高兴我能提供帮助。虽然还有很多其他的方法,但其中之一就是对现有代码进行最小的更改。什么是
    List
    ?看起来您想要收集
    WordLocation
    对象。那么,为什么要为每行调用两次
    line.toString()
    <代码>行
    已经是一个
    字符串
    。另外,
    IsleterOrdigit
    预测试已经过时,
    .equals(word)
    测试意味着这种情况。在搜索特定的
    单词时,我甚至不会使用
    BreakIterator
    。搜索单词,例如使用带有单词边界锚的
    扫描器
    ,会更简单,也可能更有效。我必须使用BreakIterator,还有其他许多条件需要保留。这是我的长代码的一部分,所以看起来很奇怪。什么是
    List
    ?看起来您想要收集
    WordLocation
    对象。那么,为什么要为每行调用两次
    line.toString()
    <代码>行
    已经是一个
    字符串
    。另外,
    IsleterOrdigit
    预测试已经过时,
    .equals(word)
    测试意味着这种情况。在搜索特定的
    单词时,我甚至不会使用
    BreakIterator
    。搜索单词,例如使用带有单词边界锚的
    扫描器
    ,会更简单,也可能更有效。我必须使用BreakIterator,还有其他许多条件需要保留。这是我长代码的一部分,所以看起来很奇怪。