|
| |||||||||
| Tags: applets, command line, console, java, servlets, standard streams |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| ||||
| ||||
| I/O from the Command Line in Java
Hi friends, I am doing the coding in Java that creates applets, servlets and general purpose applications. So I am having better knowledge in Java programming language. Now I am trying to create applications that interactively communicate with a user at a command-line. But I don't know how to do it.!! So anyone out there can please tell me about the I/O from the Command Line in Java. Hope that you guys will reply immediately.!!
__________________ ASUS P5VD1-X Intel Pentium Dual Core 3.00GHz Maxtor 160GB Corsair 1.5GB PC3200 RAM Nvidia Geforce 6800 GT 256mb Phillips DVD RW Magna 500W PSU XION II Steel Black Gaming Case |
|
#2
| ||||
| ||||
| Re: I/O from the Command Line in Java
A program is often run from the command line and interacts with the user in the command line environment. Since you know about the Java to a good extent, you should know that the Java platform supports the command line environment in following two ways :
|
|
#3
| ||||
| ||||
| Re: I/O from the Command Line in Java
There are two ways to interact with the user in Java by Standard Streams and the Console.
|
|
#4
| |||
| |||
| Re: I/O from the Command Line in Java
If you have a look on the coding then you can understand the I/O from the Command Line in Java more easily. I have given you the code that will execute a command line : Code: import java.io.*;
class trial{
public static void main(String Argv[]) {
try {
String str;
Process proc = Runtime.getRuntime().exec("ls -l");
DataInputStream input = new DataInputStream(
proc.getInputStream());
try {
while ((str = input.readLine()) != null) {
System.out.println(str);
}
} catch (IOException e) {
System.exit(0);
}
} catch (IOException e1) {
System.err.println(e1);
System.exit(1);
}
System.exit(0);
}
} |
|
#5
| |||
| |||
| Re: I/O from the Command Line in Java
The following code of an example will explain you to prompt the user to enter a String value. This value can be their name and then later you will have to read it. So have a look on the following coding : Code: import java.io.*;
public class ReadStringDemo {
public static void main (String[] args) {
System.out.print("Please enter your name here: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userName = null;
try {
userName = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error while trying to read your name!");
System.exit(1);
}
System.out.println("Your name has been entered, " + userName);
}
} |
|
#6
| ||||
| ||||
| Re: I/O from the Command Line in Java
I have given you an example that demonstrates how to read command line input from a Java program using the Scanner class : Code: import java.util.Scanner;
public class ScannerDemo
{
public static void main (String[] args)
{
System.out.print("Enter your Qualification: ");
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();
if (username.trim().equals(""))
{
System.out.println("That's not valid qualification");
}
else
{
System.out.println("Thanks for the qualification, " + username);
}
}
}
__________________ I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame. |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "I/O from the Command Line in Java" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| xcopy command line together with wmplayer command line | Aislinn | Operating Systems | 5 | 31-03-2010 01:13 PM |
| Command Line Arguments in java | Vipul03 | Software Development | 2 | 22-02-2010 04:39 PM |
| What are Command-Line Arguments in Java? | Beter 2 Burn Out | Software Development | 8 | 20-02-2010 03:37 PM |
| PROBLEM: COMMAND LINE OPTION SYNTAX ERROR. TYPE COMMAND /? FOR HEL | FORTHELOVEOFGODPLEASEHELPME | MediaCenter | 3 | 03-09-2009 06:41 PM |
| autorun.inf - shell\..\command with command line parameter doesn'twork | kakii | Windows XP Support | 1 | 18-05-2007 02:24 AM |