i-o in java.pptx

22
Input-Output in java

Upload: samsharma11

Post on 22-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: i-o in java.pptx

Input-Output in java

Page 2: i-o in java.pptx

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

Page 3: i-o in java.pptx

Input Stream

Page 4: i-o in java.pptx

Output Stream

Page 5: i-o in java.pptx

How to do I/O

import java.io.*;

Open the stream Use the stream (read, write, or both) Close the stream

Page 6: i-o in java.pptx

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.

Page 7: i-o in java.pptx

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!

Page 8: i-o in java.pptx

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. 

Page 9: i-o in java.pptx

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.

Page 10: i-o in java.pptx

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.

Page 11: i-o in java.pptx

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.

Page 12: i-o in java.pptx

Note flags and search

In between round brackets we have to tell java that this will be System Input (System.in).

Page 13: i-o in java.pptx

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();

} }

Page 14: i-o in java.pptx

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');

}

}}

Page 15: i-o in java.pptx

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();

}

}

Page 16: i-o in java.pptx

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"));

}

}

Page 17: i-o in java.pptx

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();

Page 18: i-o in java.pptx

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]);

}

}

}

Page 19: i-o in java.pptx

Demonstrate System.out.write().class WriteDemo {

public static void main(String args[]) {

int b;

b = 'A';

System.out.write(b);

System.out.write('\n');

}

}

Page 20: i-o in java.pptx

// 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);

}

}

Page 21: i-o in java.pptx

Note flags and search

Reading from a file

Page 22: i-o in java.pptx

Note flags and search

Writing to a file