A stream is a sequence of data. A stream is a flowing sequence of characters.

Slides:



Advertisements
Similar presentations
Written by Paul Pu All Rights Reservedwww.torontocollege.com 1 Input and Output.
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.
1 Streams and Input/Output Files Part I. 2 Introduction So far we have used variables and arrays for storing data inside the programs. This approach poses.
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
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
Files Files are used to store long term data. –Typically referred to as persistent data. –A file is a group of related records. –A list of friends addresses.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary I/O.
File I/O in Java CS 311, Winter File Basics Recall that a file is block structured. What does this mean? What happens when an application opens.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
CSC – Java Programming II Lecture 9 January 30, 2002.
Chapter 15: Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Use JFileChooser F Text Input.
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.
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.
Program data (instance variables, local variables, and parameters) is transient, because its lifetime ends with the program...if not, before. Sometimes.
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.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
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.
Binary Files, Random Access Files Binary Files The way data is stored in memory is sometimes called the raw binary format. Data can be stored in.
MIT AITI 2003 Lecture 15 Streams Input and Output data from/to other sources.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
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
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.
Introduction to Computation and Problem Solving Class 29: Introduction to Streams Prof. Steven R. Lerman and Dr. V. Judson Harward.
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.
Java IO. Why IO ? Without I/O, your program is a closed box. Without I/O, your program is a closed box. I/O gives your Java program access to your hard.
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.
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.
Chapter 15: Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Parsing Text Files F Random.
Chapter 15: Input and Output
Company Input/Output Stream –. Input/Output Stream Data Program Input Stream Output Stream.
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1.
Learners Support Publications Working with Files.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
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.
1 Input-Output A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated as transparently as.
Generics and file handling Presented by: Abha Kumari Neha Pradip Vhanmane Raj Visa Rashmi Kewlani Suvrat Dixit.
Java IO Exploring the java.io package and living to talk about it.
Chapter 10: I/O Streams Input Streams Output Streams
Fundamental of Java Programming
IO in java.
Sequential files creation & writing
IO in java.
Chapter 17 Input and Output
Chapter 8: Input and Output
Chapter 17 Binary I/O.
Object Writing in files
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Chapter 17 Binary I/O 1.
Programming in Java Files and I/O Streams
Streams and File I/O Chapter 9 Chapter 9.
Chapter 17 Binary I/O Dr. Clincy - Lecture.
18 File i/o, Parsing.
Stream Oriented I/O Computer Programming II Dr. Tim Margush
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Files and Streams in Java
CS 240 – Advanced Programming Concepts
Presentation transcript:

A stream is a sequence of data. A stream is a flowing sequence of characters.

Character and Byte Streams

File Class boolean exists():does the file exist boolean isFile(): is it a file boolean isDirectory: directory String[] list() :return the names of all files and directories in a directory String getName() :get the file or directory's nameString getPath() :get the file or directory's path

list(): This method of Files class returns an array of strings naming the files and directories in the directory. file.listFiles(): This method returns an array of files and folders present in the specified directory with their pathname. isDirectory(): It checks whether the file is a directory or not

public interface FilenameFilter used to filter directory listings in the list method of class File. accept public boolean accept(File dir, String name) –Tests if a specified file should be included in a file list. –Parameters: dir - the directory in which the file was found. name - the name of the file. –Returns: –true if and only if the name should be included in the file list; false otherwise. –

accept public boolean accept(File dir, String name) –Tests if a specified file should be included in a file list. –Parameters: dir - the directory in which the file was found. name - the name of the file. –Returns: true if and only if the name should be included in the file list; false otherwise.

Class FileInputStream FileInputStream: –This class is a subclass of Inputstream class –reads bytes from a specified file name. Int read(): –method reads a byte or array of bytes from the file. –returns -1 when the end-of-file has been reached. This class throws FileNotFoundException Constructor: –FileInputstream(File filename);

FilterInputStream The FilterInputStream class is the superclass of all of the input stream classes that filter input. Each of the subclasses of FilterInputStreamprovides additional functionality.

Data streams are filtered streams that perform binary I/O operation on primitive data type values ( boolean, char, byte, short, int, long, etc.) as well as on String values. If we need to work with data that is not represented as bytes or characters then you can use Data Streams. These streams filter an existing byte stream so that each primitive data types can be read from or written to the stream directly.

DataInputStream: allows you to read binary represented data of Java primitive data types from an underlying input stream in a machine-independent way DataoutputStream: allows you to write binary represented data of Java primitive data types reading from an underlying output stream in a machine- independent way. It writes only Java primitive data types and doesn't write the object values.

BufferedInputStream BufferedInputStream class that lets you read characters from a stream and stores it in an internal buffer. BufferedInputStream(InputStream inputStream) BufferedInputStream(InputStream inputStream, int bufSize)

BufferedoutputStream and buffers the output. BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream("file.java"));

Random File Access Random access files permit nonsequential, or random, access to a file's contents. read(ByteBuffer) – Reads bytes into the buffer from the channel write(ByteBuffer) – Writes bytes from the buffer to the channel. Seek :sets the file pointer getFilePointer () :reads filePointer. Int skipBytes():Add n to the file pointer.

StreamTokenizer Many times when reading an input stream of characters, we need to parse it to see if what we are reading is a word or a number. This process is called "tokenizing". The StreamTokenizer class takes an input stream and parses it into "tokens“, allowing the tokens to be read one at a time. StreamTokenizer breaks the input stream into tokens using whitespace as a delimiter. By default, Unicode characters \u0000 through \u0020 are considered whitespace. This encompasses things like space, tab, newline, etc

If you want to change this list, you need to invoke the method whitespaceChars(int low, int high); all characters having Unicode values between low and high will be considered whitespace, in addition to the default set.

int nval :If the current token is a number, this field contains the value of that number. String sval :If the current token is a word token, this field contains a string giving the characters of the word token. nextToken( ) next token is obtained from the input stream by calling

int ttype After a call to the nextToken method, this field contains the type of the token just read. Int lineno() Return the current line number. void wordChars(int low, int hi) Specifies that all characters c in the range low <= c <= high are word constituents. Void ordinaryChars(int low, int hi) Specifies that all characters c in the range low <= c <= high are "ordinary" in this tokenizer.

TT_EOF, TT_EOL, TT_NUMBER, and TT_WORD are four int constants TT_WORD indicates that the token is a word. TT_NUMBER indicates that the token is a number. TT_EOL indicates that the end of line has been read. The field can only have this value if the eolIsSignificant method has been called with the argument true. TT_EOF indicates that the end of the input stream has been reached. whitespaceChars( ) :Specify white space chars

StreamTokenizer eolIsSignificant( ) method to ensure that newline characters will be delivered as tokens, so we can count the number of lines as well as words. void eolIsSignificant(boolean eolFlag) void wordChars(int start, int end) start and end specify the range of valid characters. characters in the range 33 to 255 are valid word characters.