Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,

Slides:



Advertisements
Similar presentations
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.
Advertisements

Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lecture 15: I/O and Parsing
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
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.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Using Processing Stream. Predefined Streams System.in InputStream used to read bytes from the keyboard System.out PrintStream used to write bytes to the.
Chapter 91 Streams and File I/O Chapter 9. 2 Announcements Project 5 due last night Project 6 assigned Exam 2 –Wed., March 21, 7:00 – 8:00 pm, LILY 1105.
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.
String Tokenization What is String Tokenization?
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 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.
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
Java I/O Input: information brought to program from an external source
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.
Streams and Files CMPS Overview Stream classes File objects File operations with streams Examples in C++ and Java 2.
Two Ways to Store Data in a File Text format Binary format.
Stream: an object that either delivers data to its destination (screen, file, etc.) or that takes data from a source (keyboard, file, etc.) –it acts as.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Text Input and Output on the Console.
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.
OOP with Java, David J. Barnes Input-Output1 A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated.
JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.
MIT AITI 2003 Lecture 15 Streams Input and Output data from/to other sources.
I/O in Java Dennis Burford
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
The Java I/O Classes and Interfaces cont’d
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
Two Ways to Store Data in a File  Text format  Binary format.
© Amir Kirsh Files and Streams in Java Written by Amir Kirsh.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
Files and Streams CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Streams and File I/O Chapter 9. Outline Overview of Streams and File I/O Text-File I/O Using the File Class Basic Binary-File I/O Object I/O with Object.
1 Software 1 Java I/O. 2 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating.
Lecture 5 I/O and Parsing
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
Chapter 9 1 Chapter 9 – Part 2 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.
– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.
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.
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.
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1.
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.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
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.
1 Input-Output A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated as transparently as.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
The Java IO System Different kinds of IO Different kinds of operations
Lecture 8: I/O Streams types of I/O streams Chaining Streams
OO Design and Programming II I/O: Reading and Writing
Lesson 8: More File I/O February 5, 2008
Strings and File I/O.
12: The Java I/O System stream model.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Streams and File I/O Chapter 9 Chapter 9.
Streams and File I/O Chapter 14.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Files and Streams in Java
David Davenport Spring 2005
Presentation transcript:

based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream, OutputStream, FileOutputStream 4 Primitive-data stream files DataInputStream, DataOutputStream 4 Object stream files ObjectInputStream & ObjectOutputStream

based on OOP with Java, by David J. Barnes Input-Output2 The File Class  A File object represents an abstract pathname. –Represents both files and directories (folders). –Name, parent directory, size, permissions. –Constructor takes the file’s name: File info = new File("Letter.txt"); –No exception thrown if the file does not exist.

based on OOP with Java, by David J. Barnes Input-Output3 Methods of the File Class File info = new File("Letter.txt"); if(info.exists()){ System.out.println("Size of "+info.getName()+ " is "+info.length()); if(info.isDirectory()){ System.out.println("The file is a directory."); } if(info.canRead()){ System.out.println("The file is readable."); } if(info.canWrite()){ System.out.println("The file is writeable."); }

based on OOP with Java, by David J. Barnes Input-Output4 FileReader and FileWriter 4 The two main classes for reading and writing text files. –A file is opened when its name is passed to their constructors. –Files should be closed (via close ) when finished with. –Their read and write methods deal with char and char[].

based on OOP with Java, by David J. Barnes Input-Output5 Opening and Closing a File try{ // Try to open the file. FileReader inputFile = new FileReader(filename); // Process the file's contents.... // Close the file now that it is finished with. inputFile.close(); } catch(FileNotFoundException e){ System.out.println("Unable to open "+filename); } catch(IOException e){ // The file could not be read or closed. System.out.println("Unable to process "+filename); }

based on OOP with Java, by David J. Barnes Input-Output6 Copying a Text File static void copyFile(FileReader inputFile, FileWriter outputFile) throws IOException { final int bufferSize = 1024; char[] buffer = new char[bufferSize]; // Read the first chunk of characters. int numberRead = inputFile.read(buffer); while(numberRead > 0){ // Write out what was read. outputFile.write(buffer,0,numberRead); numberRead = inputFile.read(buffer); } outputFile.flush(); }

based on OOP with Java, by David J. Barnes Input-Output7 Reading Single Characters static void copyFile(FileReader inputFile, FileWriter outputFile){ try{ // Read the first character. int nextChar = inputFile.read(); // Have we reached the end of file? while(nextChar != -1){ outputFile.write(nextChar); // Read the next character. nextChar = inputFile.read(); } outputFile.flush(); } catch(IOException e){ System.out.println("Unable to copy a file."); }

based on OOP with Java, by David J. Barnes Input-Output8 Buffered Reader and Writers try{ FileReader in = new FileReader(infile); BufferedReader reader = new BufferedReader(in); FileWriter out = new FileWriter(outfile); BufferedWriter writer = new BufferedWriter(out);... reader.close(); writer.close(); } catch(FileNotFoundException e){ System.out.println(e.getMessage()); } catch(IOException e){ System.out.println(e.getMessage()); }

based on OOP with Java, by David J. Barnes Input-Output9 Line-by-Line Copying BufferedReader reader = new BufferedReader(...); // Read the first line. String line = reader.readLine(); // null returned on EOF. while(line != null){ // Write the whole line. writer.write(line); // Add the newline character. writer.newLine(); // Read the next line. line = reader.readLine(); }

based on OOP with Java, by David J. Barnes Input-Output10 PrintWriter try{ FileWriter out = new FileWriter(outfile); PrintWriter writer = new PrintWriter(out); writer.println(…); writer.format(“%d…”,…); writer.close(); } catch(IOException e){ System.out.println(e.getMessage()); }

based on OOP with Java, by David J. Barnes Input-Output11 System.in BufferedReader reader = new BufferedReader( new InputStreamReader(System.in));

based on OOP with Java, by David J. Barnes Input-Output12 The StringTokenizer Class  Defined in the java.util package. 4 Splits strings into separate String tokens. –Delimiter characters are user settable (whitespace by default). –Will also return delimiters if required. 4 Simple interface –public boolean hasMoreTokens() –public String nextToken()

based on OOP with Java, by David J. Barnes Input-Output13 Finding Words String line = in.readLine(); while(line != null){ StringTokenizer tokenizer = new StringTokenizer(line,",;:.\"' \t"); while(tokenizer.hasMoreTokens()){ String word = tokenizer.nextToken(); // Print the next word. System.out.println(word); } line = in.readLine(); }

based on OOP with Java, by David J. Barnes Input-Output14 The Scanner Class  Defined in the java.util package.  A scanner can be created from File, InputStream, or String.  Use useDelimiter to set the delimiting pattern.  Use next to retrieve the next token and use nextInt, etc. to retrieve a token of a certain type.

based on OOP with Java, by David J. Barnes Input-Output15 Finding Words String line = in.readLine(); while(line != null){ Scanner sc = new Scanner(line); sc.useDelimiter(",|;|:|\\.|\"|'| |\t"); while(sc.hasNext()){ String word = sc.next(); // Print the next word. System.out.println(word); } line = in.readLine(); }

based on OOP with Java, by David J. Barnes Input-Output16 The Stream Classes 4 Several classes that deliver input as a stream of raw bytes, or write raw bytes. –BufferedInputStream, FileInputStream. –BufferedOutputStream,FileOutputStream.  Their read and write methods take byte[] rather than char[] arguments.

based on OOP with Java, by David J. Barnes Input-Output17 Manipulating Primitive Data Files 4 DataInputStream –readBoolean(),readChar(),readInt()... 4 DataOutputStream –writeBoolean(),writeChar(),writeInt(),....

based on OOP with Java, by David J. Barnes Input-Output18 Reading from URLs import java.net.*; import java.io.*; public class URLReader { public static void main(String[] args) throws Exception { URL yahoo = new URL(" BufferedReader in = new BufferedReader( new InputStreamReader( yahoo.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); }

based on OOP with Java, by David J. Barnes Input-Output19 Review 4 Input-Output is usually difficult to perform in a platform-independent way.  The java.io package provides the required independence.  The File class supplies details of external files. 4 Use Reader and Writer classes for text files.

based on OOP with Java, by David J. Barnes Input-Output20 Review (cont.) 4 Use Buffered classes for input-output efficiency. 4 Use Stream classes when access to binary data is required. 4 Use DataInputStream and DataOutputStream when access to primitive data is required  StringTokenizer is useful for breaking up textual input.