JAVA字符出现在哪一行

JAVA字符出现在哪一行,java,Java,所以我一直在尝试让java接受一个输入文件,然后分析该文件并返回一些东西。目前,我正试图让它接受一个文件目录,读取该文件,然后要求用户搜索一个字符或字符串。然后,它在该文件中搜索这些字符,然后输出这些字符出现的次数,以及它们出现在哪一行。我在StackedOverflow上看到过类似的项目,并将其中的一些项目组合在一起以获得部分工作的代码。但是,下面的代码不输出字符串所在的行,只输出文件中的行总数 如何让它输出找到字符串的行 import java.util.*; import java.io.

所以我一直在尝试让java接受一个输入文件,然后分析该文件并返回一些东西。目前,我正试图让它接受一个文件目录,读取该文件,然后要求用户搜索一个字符或字符串。然后,它在该文件中搜索这些字符,然后输出这些字符出现的次数,以及它们出现在哪一行。我在StackedOverflow上看到过类似的项目,并将其中的一些项目组合在一起以获得部分工作的代码。但是,下面的代码不输出字符串所在的行,只输出文件中的行总数

如何让它输出找到字符串的行

import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.util.Scanner;

public class assign6 {
public static int countLines(String filename) throws IOException {
    InputStream is = new BufferedInputStream(new FileInputStream(filename));
    try {
        byte[] c = new byte[1024];
        int count = 0;
        int readChars = 0;
        boolean empty = true;
        while ((readChars = is.read(c)) != -1) {
            empty = false;
            for (int i = 0; i < readChars; ++i) {
                if (c[i] == '\n') {
                    ++count;
                }
            }
        }
        return (count == 0 && !empty) ? 1 : count;
    } finally {
        is.close();
    }
}
    public static void main(String args[]) throws Exception{
        boolean play = false;
        while (play == false) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter Full File Directory:");
        String filePath = s.nextLine();
        System.out.println("Enter Character or String of Characters to search for:");
        String testString = s.nextLine();

        String strLine;
        int numRead=0;

        try {
            FileInputStream fstream = new FileInputStream(filePath);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            while ((strLine = br.readLine()) != null)   {
                strLine = strLine + " ";
                String [] strArry = strLine.split(testString);

                if (strArry.length > 1) {
                     numRead = numRead + strArry.length - 1;
                }
                else {
                     if (strLine == testString) {
                         numRead++;
                     }
                }
            }

            in.close();

            System.out.println(testString + " was found " + numRead + " times.");
            System.out.println(countLines(filePath));

        }catch (Exception e){
        }
        System.out.println(" Do you want to play again: 1 for yes, 2 for no");
        Scanner z = new Scanner(System.in);
        int playagain;
        playagain = z.nextInt();
        if (playagain == 1) {
            play = false;
        }else{
            if (playagain == 2){
                play = true;
            }
        }
    }
}
}
import java.util.*;
导入java.io.*;
导入java.nio.file.*;
导入java.util.Scanner;
公开课6{
公共静态int countLines(字符串文件名)引发IOException{
InputStream is=new BufferedInputStream(new FileInputStream(filename));
试一试{
字节[]c=新字节[1024];
整数计数=0;
int readChars=0;
布尔空=真;
而((readChars=is.read(c))!=-1){
空=假;
for(int i=0;i1){
numRead=numRead+strArry.length-1;
}
否则{
if(strLine==testString){
numRead++;
}
}
}
in.close();
System.out.println(testString+“被找到”+numRead+“次”);
System.out.println(countLines(filePath));
}捕获(例外e){
}
System.out.println(“您想再次播放吗:1表示是,2表示否”);
扫描器z=新扫描器(System.in);
再次播放int;
再次播放=z.nextInt();
如果(再次播放==1){
玩=假;
}否则{
如果(再次播放==2){
玩=真;
}
}
}
}
}

您要求我们调试您的程序,如果您可以自己尝试,这是非常低效的。如果您的计数正常,您知道如何查找行,因此在出现这种情况时打印行号。其实就是这么简单。