Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



I/O from the Command Line in Java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 18-02-2010
Damien25's Avatar
Member
 
Join Date: Jul 2006
Posts: 128
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
Reply With Quote
  #2  
Old 18-02-2010
Allan.d's Avatar
Member
 
Join Date: Mar 2008
Posts: 672
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 :
  • Through the Standard Streams
  • Through the Console.
Along with creating applets, servlets, etc. Java is also useful in communicating with a user at a command-line, just like the Unix or DOS prompt.
Reply With Quote
  #3  
Old 19-02-2010
Warner's Avatar
Member
 
Join Date: Mar 2008
Posts: 349
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.
  1. Standard Streams : Standard Streams read input from the keyboard and write output to the display. There are three Standard Streams which are supported by the Java : Standard Input, Standard Error, and Standard Output. The thing that you will have to keep in mind is that these objects are defined automatically and do not need to be opened. The Standard streams are byte streams.
  2. The Console : Console can be considered as more advanced alternative to the Standard Streams. The Console is particularly useful for secure password entry. Your program must attempt to retrieve the Console object by invoking System.console() before using the Console. The secure password entry is supported by the console object through its readPassword method.
Reply With Quote
  #4  
Old 19-02-2010
Member
 
Join Date: Nov 2008
Posts: 1,193
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);
}
}
Reply With Quote
  #5  
Old 19-02-2010
Member
 
Join Date: Nov 2008
Posts: 997
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);

   }

}
Reply With Quote
  #6  
Old 19-02-2010
Solitario's Avatar
Member
 
Join Date: Aug 2006
Posts: 220
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 04:01 AM.