Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.

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.
Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Lecture 15: I/O and Parsing
Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
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.
COMP201 Java Programming Topic 5: Input and Output Reading: Chapter 12.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Files and Streams CS 21a. 10/02/05 L18: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
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.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
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.
Dynamic Arrays Dynamic arrays are arrays that are re- allocated to a larger size. –See Eck Section 8.3 for details. –For instance, what happens with the.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
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.
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.
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,
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.
Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java.
The Java I/O Classes and Interfaces cont’d
File IO Basics By Dan Fleck Coming up: Data Streams.
Introduction to Programming G50PRO University of Nottingham Unit 11 : Files Input/Ouput Paul Tennent
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.
Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be.
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
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.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Read and Write Files  By the end of this lab you will be able to:  Write a file in internal storage  Read a file from internal storage  Write a file.
– 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.
Exception Handling, Reading and Writing in Files, Serialization, Exceptions, Files, Streams, File Readers and Writers, Serializable SoftUni Team Technical.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
GENERICS AND FILE HANDLING Saumya Srivastava (977934) Divyangana Pandey (977790) Shubhi Saxena (978108) Arka Das (962969) AHD05/15-16 AJA 21.
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
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.
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.
Java IO Exploring the java.io package and living to talk about it.
The Java IO System Different kinds of IO Different kinds of operations
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Lecture 8: I/O Streams types of I/O streams Chaining Streams
CSG2H3 Object Oriented Programming
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
IO in java.
OO Design and Programming II I/O: Reading and Writing
University of Central Florida COP 3330 Object Oriented Programming
Java Exceptions and I/O
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
JAVA IO.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
18 File i/o, Parsing.
Input and Output Stream
CSC 143 Java Streams.
Files and Streams in Java
Web Design & Development Lecture 8
Presentation transcript:

Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs

Overview Java I/O –The java.io package –Streams, Readers and Writers Files –Working with Files URLs –Working with Internet Resources

Java I/O – The Basics Java I/O is based around the concept of a stream –Ordered sequence of information (bytes) coming from a source, or going to a ‘sink’ –Simplest stream reads/writes only a single byte, or an array of bytes at a time Designed to be platform-independent The stream concept is very generic –Can be applied to many different types of I/O –Files, Network, Memory, Processes, etc

Java I/O – The Basics The java.io package contains all of the I/O classes. –Many classes specialised for particular kinds of stream operations, e.g. file I/O Reading/writing single bytes is quite limited –So, it includes classes which provide extra functionality –e.g. buffering, reading numbers and Strings (not bytes), etc. Results in large inheritance hierarchy, with separate trees for input and output stream classes

Java I/O -- InputStream

Java I/O – InputStreams

Java I/O – Using InputStreams Basic pattern for I/O programming is as follows: Open a stream While there’s data to read Process the data Close the stream

Java I/O – Using InputStreams I/O in Java: InputStream in = new FileInputStream(“c:\\temp\\myfile.txt”); int b = in.read(); //EOF is signalled by read() returning -1 while (b != -1) { //do something… b = in.read(); } in.close();

Java I/O – Using InputStreams But using buffering is more efficient, therefore we always nest our streams… InputStream inner = new FileInputStream(“c:\\temp\\myfile.txt”); InputStream in = new BufferedInputStream(inner); int b = in.read(); //EOF is signalled by read() returning -1 while (b != -1) { //do something… b = in.read(); } in.close();

Java I/O – Using InputStreams We’ve omitted exception handling in the previous examples Almost all methods on the I/O classes (including constructors) can throw an IOException or a subclass. Always wrap I/O code in try…catch blocks to handle errors.

Java I/O – Using InputStreams InputStream in = null; try { InputStream inner = new FileInputStream(“c:\\temp\\myfile.txt”); in = new BufferedInputStream(inner); //process file } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (Exception e) {} }

Java I/O – OutputStream

Java I/O – OutputStreams

Java I/O – Using InputStreams Basic pattern for output is as follows: Open a stream While there’s data to write Write the data Close the stream

Java I/O – Using OutputStreams Output in Java: OutputStream out = new FileOutputStream(“c:\\temp\\myfile.txt”); while (…) { out.write(…); } out.close();

Java I/O – Using OutputStreams OutputStream out = null; try { OutputStream inner = new FileOutputStream(“c:\\temp\\myfile.txt”); out = new BufferedOutputStream(inner); //write data to the file } catch (IOException e) { e.printStackTrace(); } finally { try { out.close(); } catch (Exception e) {} }

But That’s Not All! Input/OutputStream and sub-classes were part of Java 1.1. Java 1.2 adds more classes specialised for character based I/O –The stream classes are for data I/O. Classes for character I/O are called Readers and Writers Why have specialised classes? –To support foreign languages

Unicode Each character in the ASCII character set fits into a single byte –…but that’s not enough for chinese, and other complex alphabets –Need more than a single byte –A Java character ( char ) is 2 bytes Java handles text using Unicode –International standard character set, containing characters for almost all known languages –And a few imaginary ones! (Klingon, Elvish…) Inside the JVM all text is held as Unicode

Java Text I/O Because byte != character for all languages, you have to turn bytes into chars using a Input/OutputStream Java provides Readers and Writers to save you this work. These classes deal with streams of characters –Read/write single character or array of characters –Again there are classes specialised for particular purposes

Java I/O – Reader

Java I/O – Readers

Using Readers Reader in = null; try { Reader inner = new FileReader(“c:\\temp\\myfile.txt”); in = new BufferedReader(inner); //process file } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (Exception e) {} }

Java I/O – Writer

Java I/O – Writers

Using Writers Writer out = null; try { Writer inner = new FileWriter(“c:\\temp\\myfile.txt”); out = new BufferedWriter(inner); //write data to the file } catch (IOException e) { e.printStackTrace(); } finally { try { out.close(); } catch (Exception e) {} }

Bridging the Gap Sometimes you need to bridge across the two hierachies –Use InputStreamReader or OutputStreamWriter InputStreamReader –Reads bytes from an InputStream, and turns them into characters using a character encoding OutputStreamWriter –Turns characters sent to the Writer into bytes written by the OutputStream, again using a character encoding.

The File Object Java provides access to the file system through the java.io.File object –Represents files and directories Has methods for working with files and directories –Making directories, listing directory contents –renaming and deleting, checking permissions, etc Check whether the File corresponds to a directory or a file with isDirectory() Well-featured, and intuitive –Take a look through the javadocs Quick example…

The URL Object Similar to File is the java.net.URL class –Provides access to information about website addresses Most useful is a means to open an InputStream to a remote website –Use the openStream() method Makes it very simple to retrieve files from the Internet. Throws MalformedURLException if you provide an illegal internet address in its constructor Example…

URL Object Example try { URL p = new URL(“ InputStream inner = p.openStream(); BufferedInputStream in = new BufferedInputStream(inner); //process the file… in.close() catch (Exception e) { e.printStackTrace(); }