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.

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.

Changes in I/O

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.