Results 1 to 6 of 6

Thread: I/O from the Command Line in Java

  1. #1
    Join Date
    Jul 2006
    Posts
    191

    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. #2
    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.

  3. #3
    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.

  4. #4
    Join Date
    Nov 2008
    Posts
    1,192

    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. #5
    Join Date
    Nov 2008
    Posts
    996

    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. #6
    Join Date
    Aug 2006
    Posts
    227

    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.

Similar Threads

  1. xcopy command line together with wmplayer command line
    By Aislinn in forum Operating Systems
    Replies: 5
    Last Post: 31-03-2010, 12:13 PM
  2. Command Line Arguments in java
    By Vipul03 in forum Software Development
    Replies: 2
    Last Post: 22-02-2010, 04:39 PM
  3. What are Command-Line Arguments in Java?
    By Beter 2 Burn Out in forum Software Development
    Replies: 8
    Last Post: 20-02-2010, 03:37 PM
  4. Replies: 3
    Last Post: 03-09-2009, 05:41 PM
  5. Replies: 1
    Last Post: 18-05-2007, 01:24 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,716,578,962.69356 seconds with 17 queries