i-o in java.pptx
TRANSCRIPT
Input-Output in java
Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for
data (sometimes both) An input stream may be associated with the keyboard An input stream or an output stream may be associated with a file Different streams have different characteristics:
A file has a definite length, and therefore an end Keyboard input has no specific end
Input Stream
Output Stream
How to do I/O
import java.io.*;
Open the stream Use the stream (read, write, or both) Close the stream
Using a stream
Some streams can be used only for input, others only for output, still others for both
Using a stream means doing input from it or output to it But it’s not usually that simple--you need to manipulate the
data in some way as it comes in or goes out.
Closing
A stream is an expensive resource There is a limit on the number of streams that you
can have open at one time You should not have more than one stream open on
the same file You must close a stream before you can open it again Always close your streams!
Note flags and search
input Stream Reader .
An InputStreamReader object is responsible for obtaining a stream of bytes from the keyboard, and converting them to characters for the java program to read.
Note flags and search
Buffered Reader.
A BufferedReader object can be initialized using an InputStreamReader object within the constructor(or any other subclass of java.io.Reader). The job of the BufferedReader is to "Buffer" input from the underlying Reader object, for efficient use
For example, an input stream reader can only read character by character from the keyboard. A buffered reader can buffer an entire line of input from the stream reader, until a line has been read. After that, the readLine method returns the desired token of input to the program.
Note flags and search
System.in
System.in is an InputStream which is typically connected to keyboard input of console programs.
System.in is not used as often since data is commonly passed to a command line Java application via command line arguments, or configuration files OR GUI.
Note flags and search
The "standard" input stream. This stream is already open and ready to supply input data.
This stream corresponds to keyboard input or another input source specified by the host environment or user.
Note flags and search
In between round brackets we have to tell java that this will be System Input (System.in).
Note flags and search
Import java.io.*;
public class BufferedReaderExample {
public static void main(String[]args)throws IOException{
InputStreamReader ISR = new InputStreamReader(System.in);
BufferedReader BR = new BufferedReader(ISR);
System.out.println("Enter a character?");
char userInput = (char)BR.read();
System.out.println("You enter : "+userInput);
BR.close(); ISR.close();
} }
Use a BufferedReader to read characters from the console.
import java.io.*;
class BRRead {
public static void main(String args[])
throws IOException
{
char c;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new
BufferedReader(isr);
System.out.println("Enter characters, 'q' to quit.");
// read characters
do {
c = (char) br.read();
System.out.println(c);
} while(c != 'q');
}
}}
Note flags and search
Reading String
public class BufferedReaderExample {
public static void main(String[]args)throws IOException{
InputStreamReader ISR = new InputStreamReader(System.in);
BufferedReader BR = new BufferedReader(ISR);
System.out.println("What's your name?");
String userInput = BR.readLine(); //program waits here until the user inserts a line of text
System.out.println("Your name is : "+userInput);
BR.close();
ISR.close();
}
}
Read a string from console using a BufferedReader.
import java.io.*;
class BRReadLines {
public static void main(String args[])
throws IOException
{ // create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
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"));
}
}
A tiny editor.class TinyEdit {
public static void main(String args[])
throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str[] = new String[100];
System.out.println("Enter lines of text.");
System.out.println("Enter 'stop' to quit.");
for(int i=0; i<100; i++) {
str[i] = br.readLine();
TINY EDITOR CONTINUE
if(str[i].equals("stop")) break;
}
System.out.println("\nHere is your file:");
// display the lines
for(int i=0; i<100; i++) {
if(str[i].equals("stop")) break;
System.out.println(str[i]);
}
}
}
Demonstrate System.out.write().class WriteDemo {
public static void main(String args[]) {
int b;
b = 'A';
System.out.write(b);
System.out.write('\n');
}
}
// Demonstrate PrintWriter
import java.io.*;
public class PrintWriterDemo {
public static void main(String args[]) {
PrintWriter pw = new PrintWriter(System.out, true);
pw.println("This is a string");
int i = -7;
pw.println(i);
double d = 4.5e-7;
pw.println(d);
}
}
Note flags and search
Reading from a file
Note flags and search
Writing to a file