Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Changes in I/O

                         

This is a new feature added in Java SE 6, which has the ability to read text from a terminal without echoing on the screen. This functionality is provided by java.io.Console Class which is newly added in JAVA SE 6.

Reading passwords without echoing their text
JAVA SE 6 has the new ability to read text from a terminal without echoing on the screen, through new class java.io.Console.

Console Class 
Console is a new class which is included in JAVA SE 6. It is the advanced alternative to the Standard Streams. Its a single, predefined object of Console type that provide most of the same features as by Standard Streams, and others besides. The Console is particularly useful for secure password entry. The Console object provides input and output streams which is true character stream. 

Console c=System.console();

Before using the Console, you must retrieve the Console object by invoking System.console(). This method returns the object if it is available. But if it returns NULL, then Console operations are restricted, because the OS doesn’t support them or the program was executed in non-interactive environment.

Through the readPassword method, the Console object support secure password entry. This method helps in two ways. First, it suppresses echoing, so the password is not visible on the screen. Second, readPassword returns a character array, not a String, so the password can be overwritten, removing it from memory as soon as it is no longer needed.

The Following program is for changing the user’s password and the following code demonstrates several Console methods:

import java.io.Console;
import java.util.Arrays;
import java.io.IOException;

public class UseConsole{
	static char [] oldPass;
       public static void main (String args[]) throws IOException {
        Console c = System.console();
        if (c == null) {
            System.err.println("Console Object is not available.");
            System.exit(1);
        }
        String login = c.readLine("Enter your login Name: ");
        oldPass = c.readPassword("Enter your Existing password: ");
        if (check(login, oldPass)) {
            boolean flag;
            do {
                char [] newPass1 =
                    c.readPassword("Enter your new password: ");
                char [] newPass2 =
                    c.readPassword("Enter new password again: ");
                flag = !Arrays.equals(newPass1, newPass2);
                if (flag) {
                    c.format("Mismatch the Passwords. Please try again.%n");	
                } else {
			change(newPass1);
                    c.format("Password for %s has changed.%n", login);
                }
                Arrays.fill(newPass1, ' ');
                Arrays.fill(newPass2, ' ');
            } while (flag);
        }
    }
    //check method. 
    static boolean check(String login, char[] password) {
        return true;
    }
    //change method.
    static void change(char[] password) {
		oldPass=password;
		}
}

Above Example follows these steps: 
1. Retrieving the Console object by invoking System.console(). But if the object is not available then abort from the program.
2. After getting the object invoke Console.readLine method to prompt for and read the user's login name. 
3. Invoke Console.readPassword to prompt for and read the user's existing password. 
4. Invoke check() to confirm that the user is authorized to change the password or not.
5. Repeat these three following steps until the user enters the same password twice: 
    · Invoke Console.readPassword method twice to prompt for and read a new password. 
    · If the user entered the same password both times, invoke change() to change the password. 
    · Overwrite both passwords with blanks by invoking Array.fill(). 

Output of the program is:

C:\j2se6>javac UseConsole.java 

C:\j2se6>java UseConsole
Enter your login Name: rahul
Enter your Existing password:
Enter your new password:
Enter new password again:
Mismatch the Passwords. Please try again.
Enter your new password:
Enter new password again:
Password for rahul has changed.

C:\j2se6>

Download this example.

Some new methods are also added in File are as follow:

  •   Following methods are used to retrieve the information about disk usage: 
            o getTotalSpace() This method returns the partition size in bytes 
            o getFreeSpace() This method returns the unallocated bytes of the partition 
            o getUsableSpace() This method returns the available bytes of the partition with write permission checks and OS restriction 
The following example demonstrates the above methods:
import java.io.File;
public class FileMethod {
	public FileMethod() {
        File file = new File("C:");
        System.out.println("C:");
        System.out.println("Total Space:  " + file.getTotalSpace());
        System.out.println("Free Space:   " + file.getFreeSpace());
        System.out.println("Usable Space: " + file.getUsableSpace());
        file = new File("C:\\windows");
        System.out.println("\nC:\\windows (exist)");
        System.out.println("Total Space:  " + file.getTotalSpace());
        System.out.println("Free Space:   " + file.getFreeSpace());
        System.out.println("Usable Space: " + file.getUsableSpace());
        file = new File("D:\\windows");
        System.out.println("\nD:\\windows (not exist)");
        System.out.println("Total Space:  " + file.getTotalSpace());
        System.out.println("Free Space:   " + file.getFreeSpace());
        System.out.println("Usable Space: " + file.getUsableSpace());
    }
    public static void main(String[] args) {
        new FileMethod();
    }

 Output of the program is:        

C:\j2se6>javac FileMethod.java 
C:\j2se6>java FileMethod 
C: 
Total Space: 10238640128 
Free Space: 6918594560 
Usable Space: 6918594560 

C:\windows (exist) 
Total Space: 10238640128 
Free Space: 6918594560 
Usable Space: 6918594560 

D:\windows (not exist) 
Total Space: 0 
Free Space: 0 
Usable Space: 0 

C:\j2se6> 

Download this example.

The Following new methods are used to set file permissions: 
  • public boolean setWritable(boolean writable, boolean ownerOnly) and public boolean setWritable(boolean writable) set the owner's or  everybody's write permission.

    In above methods, if writable parameter is true then it sets the permission to allow the write operations but if it is false then write operations is restricted.
    If ownerOnly parameter is true then it sets the permission to allow write operation only to owner’s, but it is false then write operations sets to everybody. But if the file system can not distinguish between owner and others then permission allow the operations to everybody.
  • public boolean setReadable(boolean readable, boolean ownerOnly) and public boolean setReadable(boolean readable) set the owner's or everybody's read permission 

    In above methods, if readable parameter is true then it sets the permission to allow the read operations but if it is false then read operations is restricted.
    If ownerOnly parameter is true then it sets the permission to allow read operation only to owner’s, but it is false then read operations sets to everybody.
  • public boolean setExecutable(boolean executable, boolean ownerOnly) and public boolean setExecutable(boolean executable) set the owner's or everybody's execute permission.

    In above methods, if executable parameter is true then it sets the permission to allow the execute operations but if it is false then execute operations is restricted.
    If ownerOnly parameter is true then it sets the permission to allow execute operation only to owner’s, but it is false then execute operations sets to everybody.
  • public boolean canExecute() This method is using to tests the value of the execute permission. This method returns true if and only if the pathname is exists and the application can execute the file.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.