KEMBAR78
Computer science input and output BASICS.pptx
I/O BASICS
Stream- It is a sequence of data/objects that supports various
methods. They are used to read or write data .
•The stream method helps to sequentially access a file
•There are two types of streams in Java-
• Byte Stream
• Character Stream.
•Byte streams in Java are used to perform input and output operations
of 8-bit bytes.
•Character stream is used to perform input and output operations for
16-bits Unicode.
•The java.io package contains classes that allow the IO in Java.
The Byte Stream Classes
•Byte streams are defined by using two class hierarchies.
•At the top are two abstract classes: InputStream and
OutputStream.
•to use the stream classes, you must import java.io.
•The abstract classes InputStream and OutputStream define several
key methods that the other stream classes implement.
•Two of the most important are read( ) and write( ), which,
respectively, read and write bytes of data.
•Both methods are declared as abstract inside InputStream and
OutputStream. They are overridden by derived stream classes.
The bytestream classes
The Character Stream Classes
Character streams are defined by using two class hierarchies.
At the top are two abstract classes, Reader and Writer.
These abstract classes handle Unicode character streams.
The abstract classes Reader and Writer define several key methods that the other stream
classes implement.
Two of the most important methods are read( ) and write( ), which read and write
characters of data, respectively
The Predefined Streams
All Java programs automatically import the java.lang package. This
package defines a class called System.
System also contains three predefined stream variables: in, out, and
err..
System.out refers to the standard output stream. By default, this is the
console.
System.in refers to standard input, which is the keyboard by default.
System.err refers to the standard error stream, which also is the
console by default.
System.in is an object of type InputStream;
System.out and System.err are objects of type PrintStream.
These are byte streams, even though they typically are used to read
and write characters from and to the console.
Reading Console Input
In Java, console input is accomplished by reading from System.in. To obtain a character based
stream that is attached to the console, wrap System.in in a BufferedReader object.
The following line of code creates a BufferedReader that is connected to the keyboard:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
After this statement executes, br is a character-based stream that is linked to the console
through System.in.
Reading Characters
To read a character from a BufferedReader, use read( ).
// Use a BufferedReader to read characters from the console.
import java.io.*;
class BRRead {
public static void main(String args[]) throws IOException {
char c;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to quit.");
// read characters
do { c = (char) br.read();
System.out.println(c);
} while(c != 'q');
} }
Here is a sample run: Enter characters, 'q' to quit. 123abcq 1 2 3 a b c q
Reading Strings
To read a string from the keyboard, use the version of readLine( )
//Read a string from console using a BufferedReader.
import java.io.*;
class RReadLines {
public static void main(String args[]) throws IOException {
String str;
BufferedReader br = new BufferedReader(new inputStreamReader(System.in));
System.out.println("Enter lines of text.");
System.out.println("Enter 'stop' to quit.");
do {
str = br.readLine();
System.out.println(str);
} while(!str.equals("stop"));
} }
Reading and Writing Data from a File
•Java FileInputStream class FileOutputStream classes used .
•Java FileInputStream class obtains input bytes from a file.
•Java FileOutputStream class writes bytes in to a file.
FileInputStream
int read() It is used to read the byte of data from the input stream.
void close() It is used to closes the stream.
Output: W
Output: Welcome to javaTpoint
FileOutputStream
Java FileOutputStream is an output stream used for writing data
to a file.
1. void write()
2. Void close()
Java FileWriter Class
Java FileWriter class is used to write character-oriented data to a file. It is character-
oriented class which is used for file handling in java.
void write(String text) It is used to write the string
into FileWriter.
void close() It is used to close the
FileWriter.
The transient and volatile Modifiers
Java defines two interesting type modifiers: transient and volatile.
These modifiers are used to handle somewhat specialized situations.
Transient
1. Transient variable is a variable that can not be serialized
2. When an instance variable is declared as transient, then its value
need not be save when an object is stored.
For example:
class T {
transient int a; // will not persist
int b; // will persist
}
Here, if an object of type T is written to a persistent storage area,
the contents of a would not be saved, but the contents of b would.
1. The volatile modifier tells the compiler that the variable
modified by volatile can be changed unexpectedly by other
parts of your program.
2. One of these situations involves multithreaded programs
3. In a multithreaded program, sometimes two or more threads
share the same variable. For efficiency considerations, each
thread can keep its own, private copy of such a shared variable.
4. The real (or master) copy of the variable is updated at various
times, such as when a synchronized method is entered.
5. While this approach works fine, it may be inefficient at times.
6. In some cases, all that really matters is that the master copy of
a variable always reflects its current state.
7. To ensure this, simply specify the variable as volatile, which
tells the compiler that it must always use the master copy of a
volatile variable
volatile modifier
Using instanceof
Java provides the run-time operator instanceof
The instanceof operator has this general form:
objref instanceof class(type)
// Demonstrate instanceof operator.
class A {
int i, j;
}
class B extends A {
int k;
}
class InstanceOf {
public static void main(String args[])
{
A a = new A();
B b = new B();
if(a instanceof A)
System.out.println("a is instance of A");
if(b instanceof B)
System.out.println("b is instance of B");
}
}

Computer science input and output BASICS.pptx

  • 1.
  • 2.
    Stream- It isa sequence of data/objects that supports various methods. They are used to read or write data . •The stream method helps to sequentially access a file •There are two types of streams in Java- • Byte Stream • Character Stream. •Byte streams in Java are used to perform input and output operations of 8-bit bytes. •Character stream is used to perform input and output operations for 16-bits Unicode. •The java.io package contains classes that allow the IO in Java.
  • 4.
    The Byte StreamClasses •Byte streams are defined by using two class hierarchies. •At the top are two abstract classes: InputStream and OutputStream. •to use the stream classes, you must import java.io. •The abstract classes InputStream and OutputStream define several key methods that the other stream classes implement. •Two of the most important are read( ) and write( ), which, respectively, read and write bytes of data. •Both methods are declared as abstract inside InputStream and OutputStream. They are overridden by derived stream classes.
  • 5.
  • 6.
    The Character StreamClasses Character streams are defined by using two class hierarchies. At the top are two abstract classes, Reader and Writer. These abstract classes handle Unicode character streams. The abstract classes Reader and Writer define several key methods that the other stream classes implement. Two of the most important methods are read( ) and write( ), which read and write characters of data, respectively
  • 8.
    The Predefined Streams AllJava programs automatically import the java.lang package. This package defines a class called System. System also contains three predefined stream variables: in, out, and err.. System.out refers to the standard output stream. By default, this is the console. System.in refers to standard input, which is the keyboard by default. System.err refers to the standard error stream, which also is the console by default. System.in is an object of type InputStream; System.out and System.err are objects of type PrintStream. These are byte streams, even though they typically are used to read and write characters from and to the console.
  • 9.
    Reading Console Input InJava, console input is accomplished by reading from System.in. To obtain a character based stream that is attached to the console, wrap System.in in a BufferedReader object. The following line of code creates a BufferedReader that is connected to the keyboard: BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); After this statement executes, br is a character-based stream that is linked to the console through System.in.
  • 10.
    Reading Characters To reada character from a BufferedReader, use read( ). // Use a BufferedReader to read characters from the console. import java.io.*; class BRRead { public static void main(String args[]) throws IOException { char c; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter characters, 'q' to quit."); // read characters do { c = (char) br.read(); System.out.println(c); } while(c != 'q'); } } Here is a sample run: Enter characters, 'q' to quit. 123abcq 1 2 3 a b c q
  • 11.
    Reading Strings To reada string from the keyboard, use the version of readLine( ) //Read a string from console using a BufferedReader. import java.io.*; class RReadLines { public static void main(String args[]) throws IOException { String str; BufferedReader br = new BufferedReader(new inputStreamReader(System.in)); System.out.println("Enter lines of text."); System.out.println("Enter 'stop' to quit."); do { str = br.readLine(); System.out.println(str); } while(!str.equals("stop")); } }
  • 12.
    Reading and WritingData from a File •Java FileInputStream class FileOutputStream classes used . •Java FileInputStream class obtains input bytes from a file. •Java FileOutputStream class writes bytes in to a file. FileInputStream int read() It is used to read the byte of data from the input stream. void close() It is used to closes the stream.
  • 13.
  • 14.
  • 15.
    FileOutputStream Java FileOutputStream isan output stream used for writing data to a file. 1. void write() 2. Void close()
  • 20.
    Java FileWriter Class JavaFileWriter class is used to write character-oriented data to a file. It is character- oriented class which is used for file handling in java. void write(String text) It is used to write the string into FileWriter. void close() It is used to close the FileWriter.
  • 22.
    The transient andvolatile Modifiers Java defines two interesting type modifiers: transient and volatile. These modifiers are used to handle somewhat specialized situations. Transient 1. Transient variable is a variable that can not be serialized 2. When an instance variable is declared as transient, then its value need not be save when an object is stored. For example: class T { transient int a; // will not persist int b; // will persist } Here, if an object of type T is written to a persistent storage area, the contents of a would not be saved, but the contents of b would.
  • 23.
    1. The volatilemodifier tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of your program. 2. One of these situations involves multithreaded programs 3. In a multithreaded program, sometimes two or more threads share the same variable. For efficiency considerations, each thread can keep its own, private copy of such a shared variable. 4. The real (or master) copy of the variable is updated at various times, such as when a synchronized method is entered. 5. While this approach works fine, it may be inefficient at times. 6. In some cases, all that really matters is that the master copy of a variable always reflects its current state. 7. To ensure this, simply specify the variable as volatile, which tells the compiler that it must always use the master copy of a volatile variable volatile modifier
  • 27.
    Using instanceof Java providesthe run-time operator instanceof The instanceof operator has this general form: objref instanceof class(type)
  • 28.
    // Demonstrate instanceofoperator. class A { int i, j; } class B extends A { int k; } class InstanceOf { public static void main(String args[]) { A a = new A(); B b = new B(); if(a instanceof A) System.out.println("a is instance of A"); if(b instanceof B) System.out.println("b is instance of B"); } }