Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

Reading Echo

Hi,
   I am trying to write a program in Java, and have been given a specific invocation of my program I need to read. This is in the form:

   echo /data/directory | java MyProgram -arguements

   What I need to do is read the information thats echoed, as this specifies a directory I need to read in my program.

Thanks,
           Chris

P.S. How do I type the | character in the Windows CMD.exe window, Shift+\ just gives the ¦ character.
 
0
Chisel
Asked:
Chisel
2 Solutions
 
zzynxSoftware engineerCommented:
0
 
bloodredsunCommented:
Which bit are you having trouble with?
>>[I] have been given a specific invocation of my program I need to read
Who by, your project manager?
0
 
zzynxSoftware engineerCommented:
>> just gives the ¦ character
afaik, that's OK. That's what is meant with |
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ChiselAuthor Commented:
Just added a new comment to that question, hope that clears up the confusion, sorry.
0
 
ChiselAuthor Commented:
The invocation is given by my university lecturer, but I have never been taught how to read information in this way, and it is not in any of my course material, I have already checked, so I thought I'd see if I could get any help here. I am not in the situation to contact him at the moment to ask him myself for help.
0
 
aozarovCommented:
System.in will read it from the pipe (as expected).
and if you need to read line by line then you can find this helpful:
DataInputStream in = new DataInputStream(System.in);
// How to read a line
//System.out.println(in.readLine());
0
 
zzynxSoftware engineerCommented:
>> echo /data/directory | java MyProgram -arguements
or
      echo /data/directory ¦ java MyProgram -arguements

means your java program should take the "/data/directory"
outputed by the command "echo /data/directory" as its input.

As aozarov said, this should help you: http://javaalmanac.com/egs/java.io/ReadFromStdIn.html
0
 
zzynxSoftware engineerCommented:
Try this:

public class MyProgram {

    public static void main(String[] args) {
     try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String str = "";
            while (str != null) {
                str = in.readLine();
                System.out.println("I got as input: " + str);
            }
        } catch (IOException e) {
        }
    }
}
0
 
ChiselAuthor Commented:
Thanks zzynx, your code worked great. Gave 30 point to aozarov as although his code would have worked it was rejected as deprecated by javac.
0
 
zzynxSoftware engineerCommented:
thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now