CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Lecture 15: I/O and Parsing
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
Java I/O and Java Networking (Client Side) Yoshi.
1 Lecture 4 Exception Handling. 2 Exception-Handling Fundamentals An exception is an abnormal condition that arises in a code sequence at run time A Java.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Io package as Java’s basic I/O system continue’d.
Java I/O Input: information brought to program from an external source
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
CIS 068 JAVA I/O: Streams and Files. CIS 068 I/O Usual Purpose: storing data to ‘nonvolatile‘ devices, e.g. harddisk Classes provided by package java.io.
Two Ways to Store Data in a File Text format Binary format.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
I/O in Java Dennis Burford
Files Chap. 10 Streams, Readers, Writers 1. 2 Problem In our array example, we entered the students' names and scores from the keyboard. In many situations.
Overloading There is another aspect to polymorphism: Overloading Overloading is not overriding. In Turkish: Overridding: eskisini (geçersiz kılmak) Overloading:
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
File IO Basics By Dan Fleck Coming up: Data Streams.
Two Ways to Store Data in a File  Text format  Binary format.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
Lecture 5 I/O and Parsing
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
Java Input/Output. Java Input/output Input is any information that is needed by your program to complete its execution. Output is any information that.
Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
CSI 3125, Preliminaries, page 1 Files. CSI 3125, Preliminaries, page 2 Reading and Writing Files Java provides a number of classes and methods that allow.
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
CS 116 Object Oriented Programming II Lecture 11 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
java.io supports console and file I/O
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Lecture 8: I/O Streams types of I/O streams Chaining Streams
OO Design and Programming II I/O: Reading and Writing
Text File Input/Output
Strings and File I/O.
12: The Java I/O System stream model.
I/O Basics.
File.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
I/O Streams- Basics Byte Streams and Character Streams
Streams and File I/O Chapter 14.
JAVA IO.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Input and Output Stream
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Web Design & Development Lecture 8
ECE 122 April 14, 2005.
EEC 484/584 Computer Networks
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Presentation transcript:

CSI 3125, Preliminaries, page 1 Java I/O

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.

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.

CSI 3125, Preliminaries, page 4 Java I/O

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.

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

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.

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

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

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)

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

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.

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 != ‘*');

CSI 3125, Preliminaries, page 14 Reading String readLine( ) that is a member of the BufferedReader class. Syn String readLine( ) throws IOException

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

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

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

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

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