-1
String[] cmd = {
                "/bin/bash",
                "-c",
                "python count_freqs.py gene.train > gene_counts2.txt",
        };
        Runtime.getRuntime().exec(cmd);
        bufferReader.close();
        fileReader.close();
        FileReader fileReader2 =  new  FileReader("/home/mordor/workspace/GeneNamesInBiologicalText/gene_counts2");
        String newLine ;
        try (BufferedReader br = new BufferedReader(new FileReader("/home/mordor/workspace/GeneNamesInBiologicalText/gene_counts2.txt"))) {
            String line1;
            while ((line1 = br.readLine()) != null) {
              System.out.println(line1);
            }
        }*/

I used the String cmd[] to build a script to create a file called gene_Counts2.text which it successfully does and it's populated with a lot of text.

But when I use a filereader and bufferreader to iterate through the text and print it, it doesn't happen cause line1 from the code is null.

However once the program has finished running I tried iterating through the file and it works.

So how do I access the contents of the file without having to restart my program?

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
Praveen
  • 1
  • 1

2 Answers2

2

You should use Runtime.getRuntime().exec(cmd).waitFor(); to wait for the command to finish.

Also, it seems that you're creating a FileReader fileReader2 which you never use

Titus
  • 22,031
  • 1
  • 23
  • 33
  • hey thanks Titus, that worked ! yea about the filereaders, I was editing the code quite a bit to find a solution forgot to remove it, but thanks for pointing it out ! – Praveen Dec 24 '15 at 07:37
0

Code you better use:

String[] cmd = {
            "/bin/bash",
            "-c",
            "python count_freqs.py gene.train > gene_counts2.txt",
    };
    Runtime.getRuntime().exec(cmd).waitFor(); // wait until command gets terminated
    bufferReader.close();
    fileReader.close();
    FileReader fileReader2 =  new  FileReader("/home/mordor/workspace/GeneNamesInBiologicalText/gene_counts2.txt"); // never used, repair line
    String newLine; // coding properly
    try (BufferedReader br = new BufferedReader(new FileReader("/home/mordor/workspace/GeneNamesInBiologicalText/gene_counts2.txt"))) {
        String line1;
        while ((line1 = br.readLine()) != null) {
          System.out.println(line1);
        }
    }*/

You should also use ProcessBuilder.