CS 240 – Advanced Programming Concepts

Slides:



Advertisements
Similar presentations
Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Advertisements

Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
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.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary I/O.
CIS 234: More File Input & Output Dr. Ralph D. Westfall May, 2007.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Chapter 17 Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams  Use JFileChooser F Text Input.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
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,
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.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
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.
Introduction to Computation and Problem Solving Class 29: Introduction to Streams Prof. Steven R. Lerman and Dr. V. Judson Harward.
A stream is a sequence of data. A stream is a flowing sequence of characters.
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.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
Copyright(c) Systems and Computer Engineering, Carleton Univeristy, * Object-Oriented Software Development Unit 13 I/O Stream Hierarchy Case.
Streams & Files. Java I/O Classes Saving data to / Loading data from files Two Choices: Binary-Formatted or Text-Formatted Data – int x = 1234; – Binary.
– 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.
1 Chapter 19 Binary I/O. 2 Motivations F Data stored in a text file – is represented in human-readable form –Text file –Readable –Java source programs.
DEPARTMENT OF COMPUTER SCIENCE N.HARIKA. Contents Overview of I/O Streams Character Streams Byte Streams Using the Streams 2.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 13 Java Fundamentals File I/O Serializing an.
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 Programming: Advanced Topics 1 Input/Output and Serialization.
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.
Chapter 10: I/O Streams Input Streams Output Streams
java.io supports console and file I/O
The Java IO System Different kinds of IO Different kinds of operations
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Fundamental of Java Programming
IO in java.
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.
Chapter 17 Input and Output
Chapter 17 Binary I/O.
Java Exceptions and I/O
I/O Basics.
Chapter 17 Binary I/O 1.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Programming in Java Files and I/O Streams
Java’s Hierarchy Input/Output Classes and Their Purpose
Java Programming Course
Chapter 17 Binary I/O Dr. Clincy - Lecture.
JAVA IO.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Input and Output in Java
Input and Output Stream
Input and Output in Java
Input and Output in Java
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Files and Streams in Java
David Davenport Spring 2005
Presentation transcript:

CS 240 – Advanced Programming Concepts Streams & Files CS 240 – Advanced Programming Concepts

Ways to Read Files Streams Scanner Class Files Class RandomAccessFile Class

Java I/O Streams (overview) Writing data to / Reading data from files (or other data sources) Two Choices: Binary-Formatted or Text-Formatted Data Binary Formatted: 00 00 04 D2 (4 bytes) Text Formatted: 1234 (4 characters) InputStream and OutputStream Reading/writing bytes Reading/writing binary-formatted data Reader and Writer Reading/writing characters Reading/writing text-formatted data

Other Java I/O Classes (overview) File Represents a file in the file system but not directly used to read it Scanner Tokenize stream input (read one token at a time) Files Read, copy, etc whole files RandomAccessFile Use a file pointer to read from / write to any location in a file

Reading/Writing Bytes The InputStream interface is used to read bytes sequentially from a data source FileInputStream PipedInputStream URLConnection.getInputStream() HttpExchange.getRequestBody() ResultSet.getBinaryStream(int columnIndex) Many more examples in the Java API

Filter Input Streams There are many features you may want to enable when consuming data from an InputStream Decompress data as it comes out of the stream Decrypt data as it comes out of the stream Compute a “digest” of the stream (a fixed-length value that summarizes the data in the stream) Byte counting Line Counting Buffering

Filter Input Streams Open an InputStream on a data source (file, socket, etc.), and then wrap it in one or more “filter input streams” that provide the features you want (decompression, decryption, etc.) Filter input streams all implement the InputStream interface, and can be arranged as a pipeline with the real data source at the end Each filter input stream reads data from the next InputStream in line, and then performs a transformation or calculation on the data

OutputStream Writing bytes works the same way, just in reverse The OutputStream interface is used to write bytes sequentially to a data destination FileOutputStream PipedOutputStream URLConnection.getOutputStream() HttpExchange.getResponseBody() Many more examples in the Java API

Filter Output Streams There are many features you may want to enable when writing data to an OutputStream Compress data as it goes into the stream Encrypt data as it goes into the stream Compute a “digest” of the stream (a fixed-length value that summarizes the data in the stream) Buffering

Filter Output Streams Open an OutputStream on a data destination (file, socket, etc.), and then wrap it in one or more “filter output streams” that provide the features you want (compression, encryption, etc.) Filter output streams all implement the OutputStream interface, and can be arranged as a pipeline with the real data destination at the end Each filter output stream performs a transformation or calculation on the data, and then writes it to the next OutputStream in line

Filter Stream Example Compress a file to GZIP format Compress Example LegacyCompress Example Decompress a file from GZIP format Decompress Example LegacyDecompress Example

Reading/Writing Binary-Formatted Data Reading/writing bytes is useful, but usually we want to read/write larger data values, such as: float, int, boolean, etc. The DataOutputStream class lets you write binary-formatted data values The DataOutputStream(OutputStream out) constructor wraps a DataOutputStream around any OutputStream The DataInputStream class lets you read binary-formatted data values The DataInputStream(InputStream in) constructor wraps a DataInputStream around any InputStream

Reading/Writing Characters The Reader interface is used to read characters sequentially from a data source The Writer interface is used to write characters sequentially to a data destination See CopyFileExample (from Java Fundamentals lecture) Convert between streams and readers or writers using InputStreamReader and OutputStreamWriter new InputStreamReader(new FileInputStream(“myfile.txt”)); new OutputStreamWriter(new FileOutputStream(“myfile.txt”));

Reading/Writing Text-Formatted Data The PrintWriter class lets you write text-formatted data values (String, int, float, boolean, etc.) See CopyFileExample (from Java Fundamentals lecture) The Scanner class lets you read text-formatted data values

Scanner: Tokenize Data Read from a File public void processFile(File file) throws IOException { Scanner scanner = new Scanner(file); // Delimit by whitespace and # line comments scanner.useDelimiter("((#[^\\n]*\\n)|(\\s+))+"); while(scanner.hasNext()) { String str = scanner.next(); // Do something with the String }

Files: Read Entire File into a List public List<String> readFile(File file) throws IOException { Path path = Paths.get(file.getPath()); List<String> fileContents = Files.readAllLines(path); return fileContents; }

Random Access Files The RandomAccessFile class allows “random access” to a file’s contents for both reading and writing Random Access The “file pointer” represents the current location in the file (similar to an array index) Use the seek(long) or skipBytes(int) methods to move the “file pointer” to any location in the file Read or write bytes at the current file pointer location using various overloaded read and write methods