Harbinger
Harbinger

Reputation: 762

Unix count line of file command - from java code

I'm trying to count the number of lines of a text file using a unix command from java code.

My code looks like:

String filePath = "/dir1/testFile.txt";

Runtime rt = Runtime.getRuntime();
Process p;
try {
    System.out.println("No: of lines : ");
    findLineCount = "cat " + filePath + " | wc -l";
    p = rt.exec(findLineCount);
    p.waitFor();
} catch (Exception e) {
    //code
}

But, nothing is displayed in the console. When I execute the command directly, it works. What could be the issue in the above code?

Upvotes: 4

Views: 1406

Answers (3)

Niraj Sonawane
Niraj Sonawane

Reputation: 11095

This is how i printed number of lines

public static void main(String[] args) {
        try {
            Runtime run = Runtime.getRuntime();
            String[] env = new String[] { "path=%PATH%;" + "your shell path " }; //path of  cigwin  bin or any similar application. this is needed only for windows  
            Process proc = run.exec(new String[] { "bash.exe", "-c", "wc -l < yourfile" }, env);
             BufferedReader reader = new BufferedReader(new InputStreamReader(        
                     proc.getInputStream()));                                          
                    String s;                                                                
                    while ((s = reader.readLine()) != null) {                                
                      System.out.println("Number of lines " + s);                             
                    }                                           

            proc.waitFor();
            int exitValue = proc.exitValue();
            System.out.println("Status {}" +  exitValue);


        }  catch (IOException | InterruptedException e) {
            e.printStackTrace();
        } 
    }

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201477

I suggest you use a ProcessBuilder instead of Runtime.exec. You can also simplify your command by passing the filePath to wc. Please don't swallow Exception(s). Finally, you can use ProcessBuilder.inheritIO() (Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process) like

String filePath = "/dir1/testFile.txt";
try {
    System.out.println("No: of lines : ");
    ProcessBuilder pb = new ProcessBuilder("wc", "-l", filePath);
    pb.inheritIO();
    Process p = pb.start();
    p.waitFor();
} catch (Exception e) {
    e.printStackTrace();
}

Of course, it's more efficient to count the lines in Java without spawning a new process. Perhaps like,

int count = 0;
String filePath = "/dir1/testFile.txt";
try (Scanner sc = new Scanner(new File(filePath));) {
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        count++;
    }
} catch (Exception e) {
    e.printStackTrace();
}
System.out.printf("No: of lines : %d%n", count);

Upvotes: 3

Chris Martin
Chris Martin

Reputation: 30746

When I execute the command directly

I doubt you're execute it "directly". You're probably running it in a shell.

Your code should run that script in a shell too.

rt.exec(new String[]("bash", "-c", findLineCount});

Upvotes: 2

Related Questions