?
Solved

Reading Echo

Posted on 2005-03-23
10
Medium Priority
?
282 Views
Last Modified: 2010-03-31
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
Comment
Question by:Chisel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 13611892
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13611901
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13611910
>> 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.

 

Author Comment

by:Chisel
ID: 13611925
Just added a new comment to that question, hope that clears up the confusion, sorry.
0
 

Author Comment

by:Chisel
ID: 13611955
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
 
LVL 15

Assisted Solution

by:aozarov
aozarov earned 120 total points
ID: 13611956
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13612042
>> 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
 
LVL 37

Accepted Solution

by:
zzynx earned 480 total points
ID: 13612079
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
 

Author Comment

by:Chisel
ID: 13612152
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13612185
thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question