Results 1 to 7 of 7
  1. #1
    freephoneid is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default Input redirection in Java

    Hi,
    I've a perl script called process.pl which I use to invoke through unix client as shown below:

    $ process < abc.txt

    Now, abc.txt contains entries per line which is processed by this perl script.

    I want to invoke this process script through Java. I've captured the input which is required for this script in StringBuffer. My question is how do I pass this input which I stored in stringbuffer to this script?

    I've tried below command, but it takes only 1st string within my Stringbuffer.

    Stringbuffer str; // This contains line by line input which is required for script
    .....
    Runtime.getRuntime().exec("process " + str);

    But the above code executes & parses only 1st line in my stringbuffer. Can any one provide me the code which will take all input from this stringbuffer & pass it to process script??

    Please note that I DO NOT want to create a file & then pass it to script.

    Thanks in Advance!

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    12

    Default

    Process.getInputStream() and Process.getOutputStream() to communicate with an external process.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    freephoneid is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Can you please provide me the code snipppt to pass stringbuffer to this output or input stream?

    Thanks in Advance!

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    12

    Default

    Wrap the stream with a BufferedWriter and write() the contents of the StringBuffer.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    freephoneid is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Hi OrangeDog,
    I really appreciate your response however, I'm not expert in Java.

    So far, I've below code:
    Java Code:
    StringBuffer str= new StringBuffer();
    str.append("1st line");
    str.append("\n");
    str.append("2nd line");
     
    Process process = Runtime.getRuntime().exec("process " + str);
    OutputStream outStream = process.getOutputStream();
     
    int exitVal = process.waitFor();
     
    if (exitVal == 0) {
         result = true;
    } else {
         result = false;
    }
    How do I write data from stringbuffer to outstream?

    Appreciate any help!!

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    12

    Default

    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,309
    Blog Entries
    1
    Rep Power
    32

Similar Threads

  1. redirection
    By prashant in forum Networking
    Replies: 1
    Last Post: 02-25-2009, 03:53 PM
  2. Output Redirection
    By Sixtease in forum New To Java
    Replies: 8
    Last Post: 12-29-2008, 11:18 AM
  3. JSP redirection script
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-07-2008, 11:33 AM
  4. redirection in jsp
    By valery in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-06-2007, 09:31 PM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 09:46 AM

Posting Permissions

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