Methods of Inputstream Class

Method

Table 3.3. Runtime Class

Example 3.24: Using the exec function

class RuntimeDemo

{


public static void main(String args[])

{


Runtime r = Runtime.getRuntime(); Process p = null;

try {

p = r.exec(“calc.exe”);


}

catch(Exception e)

{

System.out.println(“Error executing calculator”);

}

}

}


You can get a reference to the current Runtime via the Runtime.getRuntime() method.

You can then run the calc.exe program and reference calc.exe in the Process object.

h) System class

The System class provides facilities such as, standard input, standard output, and error stream. It also provides methods to access properties related to the Java Runtime system, and various environment properties such as, version, path, or services, etc. The fields of this class are in , out , and err , which represent standard input, standard output, and error respectively.

The following table describes the methods of this class:


Method

Purpose

exit(int)

Stops execution, and returns the value of the code. 0 indicates that it is okay to exit normally.

gc()

Call the garbage collection department.

getProperties()

Returns properties of the Java runtime system.

setProperties()

Set current system properties.

currentTimeMillis()

Returns the current time in milliseconds (ms),

calculated from 0:00 on January 1, 1970.

arrayCopy(Object, int, Object, int, int)

Copy array.

Maybe you are interested!

Table 3.4. System class.

The System class cannot be instantiated.

Example 3.22: Read and display some Java environment properties.

class SystemDemo

{


public static void main(String args[])

{


System.out.println(System.getProperty(“java.class.path”)); System.out.println(System.getProperty(“java.home”)); System.out.println(System.getProperty(“java.class.version”));

System.out.println(System.getProperty(“java.specification.vendor”)); System.out.println(System.getProperty(“java.specification.version”)); System.out.println(System.getProperty(“java.vendor”)); System.out.println(System.getProperty(“java.vendor.url”)); System.out.println(System.getProperty(“java.version”)); System.out.println(System.getProperty(“java.vm.name”));

}

}


Each property to be printed needs to be provided as a parameter (in string form) to the System.getProperty() method. This method will return the corresponding information and the System.out.println() method will print it to the screen.

The results of the above program are as follows:



Figure 3.2. Result of running example 3.22

i) Class


Instances of this class contain the runtime state of an object in a running Java application. This allows us to access information about the object at runtime.

We can get an object of this class, or an instance, in one of three ways:

Use the object's getClass() method.

Use the static forName() method of a class to get an instance of a class by the name of that class.

Use a ClassLoader object to load a new class. The Class class does not have a constructor.

Example 3.23: Illustrate how to use a class's method to access information of that class:

interface A

{


final int id = 1;

final String name = “Diana”;

}


class B implements A

{


int deptno;

}


class ClassDemo

{

public static void main(String args[])

{


A a = new B(); B b = new B(); Class x;

x = a.getClass();

System.out.println(“a is object of type: ”+x.getName()); x= b.getClass();

System.out.println(“b is object of type: ”+x.getName()); x=x.getSuperclass();

System.out.println(x.getName()+ “is the superclass of b.”);

}

}


The result of running the program is described as shown below:

Figure 3.3. Result of running example 3.23

j) Object class


The Object class is a parent class of all classes. Although a user-defined class does not inherit any other class, by default it inherits the Object class.

Some of the methods of the Object class are shown below:


Method

Purpose

equals(Object)

Compare the current object with another object.

finalize()

Final method. Usually overridden in subclass.

notify()

Notifies the Thread that is currently in the waiting state.

notifyAll()

Notifies all current Threads in waiting state.

toString()

Returns a string representation of the object.

wait()

Put the Thread into a waiting state.

Table 3.5. Object class.

In the following program, we are not declaring any class or package. Now, we can create using equals() method. Because by default ObjectDemo class extends Object class.

Example 3.24: ObjectDemo class

Class ObjectDemo

{


public static void main(String args[])

{


if (args[0].equals(“Aptech”))

System.out.println(“Yes, Aptech is the right choice!”);

}

}

3.2. Streams

In general terms, a stream is a flow of data. In technical terms, a stream is a path along which data is passed through a program. One familiar application of streams is the System.in input stream.

Streams are pipelines for sending and receiving information in java programs. When a stream of data is sent or received, we refer to it as “writing” and “reading” a stream respectively. When a stream is read or written, other threads that are trying to read/write that stream are suspended. If an error occurs while reading or writing a stream, an IOException is thrown. Therefore, stream statements must include a try-catch block.

The class „java.lang.System‟ defines the standard input and output streams. They are the main classes of byte streams that java provides. We have also used output streams to output data and display the results on the screen. The input/output streams include:

System.out class: Standard output stream used to display results on the screen.

System.in class: Standard input usually comes from the keyboard and is used to read data characters.

System.err class: This is the standard error line.

The „InputStream‟ and „OutputStream‟ classes provide various I/O capabilities. Both classes have derivatives to perform I/O through buffers, files, and pipelines. Subclasses of the InputStream class perform input, while subclasses of the OutputStream class perform output.

3.3. Java.io Package

System threads are very useful. However, they are not powerful enough to handle real I/O. The java.io package must be imported for this purpose. We will discuss the classes in the java.io package.

3.3.1. InputStream class

The InputStream class is an abstract class. It defines how data is received. The important thing is not where the data comes from, but how accessible it is. The InputStream class provides several methods for reading and using streams of data as input. These methods help us create, read, and process input streams. The methods are shown in the following table:

Describe

read()

Reads bytes of data from a stream. If there are no bytes of data, it must wait. When a method must wait, the executing threads must pause until there is data.

read(byte[])

Returns the number of bytes read or „-1‟ if the end of the line has been read. It raises an IOException if an error occurs.

read(byte[], int, int)

It also reads into a byte array. It returns the number of bytes actually read until the end of the line. It raises an IOException if an error occurs.

available()

This method returns the number of bytes that can be read without waiting. It returns the current number of bytes in the stream. It is not a reliable method for performing input processing.

close()

This method closes the stream. It is used to release any used stream resources. Always close a stream to ensure that the stream processing is finished. It raises an IOException if an error occurs.

mark()

Mark the current position of the line.

markSupported()

Returns a boolean value indicating whether the stream supports mark and reset capabilities. It returns True if the stream supports it otherwise returns False.

reset()

This method repositions the line to the last typed position. It raises an IOException if an error occurs.

skip()

This method skips „n‟ bytes of the input stream. ‟-n‟ specifies the number of bytes to skip. It raises an IOException if an error occurs. This method is used to move to a specific position within the input stream.

Method name

Table 3.6. Methods of the InputStream class

3.3.2. OutputStream class

The OutputStream class is also an abstract class. It defines how output is written to a stream. It provides a set of helper methods to create, write, and process output streams. The methods include:

Method name

Describe

write(int)

This method writes a byte

write(byte[])

This method blocks until a byte is written. The stream must wait until the write operation is complete. It

raises an IOException if an error occurs.

write(byte[],int,int)

This method writes an array of bytes. The OutputStream class defines three different forms of the method that can write a single byte, an array of bytes, or a segment of a byte array.

flush()

This method clears the line.

Buffers data to be written to the stream. It throws an IOException if an error occurs.

close()

Line closing method.

It is used to release any resources associated with the stream. It throws an IOException if an error occurs.

Table 3.7. OutputStream class methods

3.3.3. Byte array input and output

The „ByteArrayInputStream‟ and „ByteArrayOutputStream‟ classes use buffers. It is not necessary to use them together.

ByteArrayInputStream class

This class creates an input stream from a buffer, which is an array of bytes. This class does not support new methods. Instead, it overrides the methods of the InputStream class such as „read()‟, „skip()‟, „available()‟ and „reset()‟.

ByteArrayOutputStream class

This class creates an output stream on an array of bytes. It also provides the ability to grow the output array for increasing its size. This class also provides the „toByteArrray()‟ and „toString()‟ methods. These are used to convert the stream to a byte array or string.

Comment


Agree Privacy Policy *