Download presentation
Presentation is loading. Please wait.
Published byNatalie Alexander Modified over 8 years ago
1
CSI 3125, Preliminaries, page 1 Java I/O
2
CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce the output based on the input. Java uses the concept of stream to make I/O operation fast. The java.io package contains all the classes required for input and output operations. A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.
3
CSI 3125, Preliminaries, page 3 Java I/O OutputStream Java application uses an output stream to write data to a destination, it may be a file,an array,peripheral device or socket. InputStream Java application uses an input stream to read data from a source, it may be a file,an array,peripheral device or socket.
4
CSI 3125, Preliminaries, page 4 Java I/O
5
CSI 3125, Preliminaries, page 5 Byte Streams Programs use byte streams to perform input and output of 8-bit bytes Byte streams provide a convenient means for handling input and output of bytes. Byte streams are used, for example, when reading or writing binary data. There many classes related to byte streams but the most frequently used classes are, FileInputStream andFileOutputStream.
6
CSI 3125, Preliminaries, page 6 Byte Streams Example copy content of file input.txt to output.txt import java.io.*; public class CopyFile { public static void main(String args[]) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("input.txt"); out = new FileOutputStream("output.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } }finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } }}
7
CSI 3125, Preliminaries, page 7 Character Streams Character streams are used to perform input and output for 16-bit unicode. most frequently used classes are, FileReader and FileWriter.. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.
8
CSI 3125, Preliminaries, page 8 Character Streams re-write above example import java.io.*; public class CopyFile { public static void main(String args[]) throws IOException { FileReader in = null; FileWriter out = null; try { in = new FileReader("input.txt"); out = new FileWriter("output.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } }finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } }
9
CSI 3125, Preliminaries, page 9 Standard Streams All the programming languages provide support for standard I/O All these streams are attached with console. 1) System.out: standard output stream 2) System.in: standard input stream 3) System.err: standard error stream example System.out.println("simple message"); System.err.println("error message"); int i=System.in.read();// returns ASCII code of 1st character System.out.println((char)i);//will print the character
10
CSI 3125, Preliminaries, page 10 Reading Console Input In Java 1.0, the only way to perform console input was to use a byte stream, Java 2 is to use a character-oriented stream 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, to create a character stream. BuffereredReader supports a buffered input stream. Its most commonly used constructor is shown here: BufferedReader(Reader inputReader)
11
CSI 3125, Preliminaries, page 11 Reading Console Input To obtain an InputStreamReader object that is linked to System.in, use the following constructor: InputStreamReader(InputStream inputStream) Because System.in refers to an object of type InputStream, it can be used for inputStream. Putting it all together, 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. Create BufferedReader from InputStreamReader and System.in, read console input
12
CSI 3125, Preliminaries, page 12 Reading Charater BufferedReader, use read( ). int read( ) throws IOException Each time that read( ) is called, it reads a character from the input stream and returns it as an integer value. It returns –1 when the end of the stream is encountered. As you can see, it can throw an IOException.
13
CSI 3125, Preliminaries, page 13 Reading Character import java.io.*; class input { public static void main(String args[])throws Exception { char c; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a character "); c = (char) br.read(); System.out.println(c); } read characters until ‘*’ do { c = (char) br.read(); System.out.println(c); } while(c != ‘*');
14
CSI 3125, Preliminaries, page 14 Reading String readLine( ) that is a member of the BufferedReader class. Syn String readLine( ) throws IOException
15
CSI 3125, Preliminaries, page 15 Reading String import java.io.*; class dd { public static void main(String args[])throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a character "); String s = br.readLine(); System.out.println(s); } Read strings until “stop” do { str = br.readLine(); System.out.println(str); } while(!str.equals("stop"));
16
CSI 3125, Preliminaries, page 16 Reading integer Need conversion import java.io.*; class dd { public static void main(String args[])throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a Integer "); int s = Integer.parseInt(br.readLine()); System.out.println(s); } For double double r=Double.parseDouble(br.readLine());
17
CSI 3125, Preliminaries, page 17 PrintWriter Class PrintWriter is one of the character-based classes. Using a character-based class for console output PrintWriter supports the print( ) and println( ) methods To write to the console by using a PrintWriter, specify System.out for the output stream and flush the stream after each newline. ex PrintWriter pw = new PrintWriter(System.out, true);
18
CSI 3125, Preliminaries, page 18 PrintWriter Class import java.io.*; class W { public static void main(String args[]) { int b=10; PrintWriter pw = new PrintWriter(System.out, true); pw.println("Value of b="+b); }
19
CSI 3125, Preliminaries, page 19 Lab Questions Find Length of the given String Concatenate 2 given Strings Convert the given String to uppercase ( String object.toUpperCase() ) Check the given integer is even or odd Biggest among given 3 nos Calculate Grade of a student by inputting Name, Rollno, and 3 marks (A+,A,B+,B,C) Reverse the given no Biggest among N given nos Check the given no is prime or not
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.