Results 1 to 3 of 3
  1. #1
    manik is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Exclamation Probelm reading text file from unix

    Hi ,

    I want to read a text file present in Unix machine using java code . I wrote the following java code and shell script to do that . But i am getting only last few lines of the file . Is there any other way to do this . I can use simple java code to read a file but i need to run two commands using pipe both cat and grep this is not possible with only java so i used shell script to do that


    String script= "/ViewFile.ksh";
    String cmdarr[] = {"sh","-C",script};
    Process p = null;
    p = Runtime.getRuntime().exec(cmdarr);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((s = stdInput.readLine()) != null) {
    logFile.append(s);
    logFile.append("\n");
    }

    in the script file i have some thing like this
    ViewFile.ksh

    cat /file.tmp | grep 'Hello'

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    File (Java Platform SE 6).
    FileReader.
    Just a possibility...

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    That's not much to go on, but it sounds like a race condition. If I understand, you shell out, which causes your script to write to write to stdin, and then you set up to read stdin. Sounds like what is written to stdin is lost if it is not read. The read process doesn't start until most of the file has been lost.

    I would suggest writing the output to a new file, waiting until the commands complete, and then opening the new file and reading it. Or, is that what this is supposed to do? I'm not sure you need a Java program to do that...

Similar Threads

  1. Reg: Reading Files from Text file
    By balaji csc in forum New To Java
    Replies: 1
    Last Post: 11-09-2009, 11:06 AM
  2. Reading & Analyzing a Text file and
    By A.M.S in forum New To Java
    Replies: 1
    Last Post: 11-07-2009, 06:47 PM
  3. reading data from text file .. help plz
    By Thug heart in forum New To Java
    Replies: 7
    Last Post: 02-15-2009, 07:29 PM
  4. Reading two text file and sum them up
    By matt_well in forum New To Java
    Replies: 36
    Last Post: 07-22-2008, 02:55 AM
  5. Reading text file
    By Lennon-Guru in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 11:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •