Files and Streams The material in this chapter is not tested on the AP CS exams.

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

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.
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 20: Streams.
Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
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.
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.
Standard input, output and error. Lecture Under Construction.
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.
Streams and Files The objectives of this chapter are: To understand the principles of I/O streams and where to use them To understand the options and limitations.
Java I/O and Java Networking (Client Side) Yoshi.
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.
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.
Java I/O Input: information brought to program from an external source
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
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.
I / O in java. java.io BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter ByteArrayInputStream ByteArrayOutputStream CharArrayReader.
Operating Systems COMP 4850/CISG 5550 File Systems Files Dr. James Money.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Java Input/Output CSE301 University of Sunderland Harry Erwin, PhD Half Lecture.
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.
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.
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.
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.
1 Exceptions Exception handling – Exception Indication of problem during execution – E.g., divide by zero – Chained exceptions Uses of exception handling.
Company Input/Output Stream –. Input/Output Stream Data Program Input Stream Output Stream.
DEPARTMENT OF COMPUTER SCIENCE N.HARIKA. Contents Overview of I/O Streams Character Streams Byte Streams Using the Streams 2.
Exception Handling, Reading and Writing in Files, Serialization, Exceptions, Files, Streams, File Readers and Writers, Serializable SoftUni Team Technical.
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.
 Pearson Education, Inc. All rights reserved Files and Streams.
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.
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.
java.io supports console and file I/O
The Java IO System Different kinds of IO Different kinds of operations
CSG2H3 Object Oriented Programming
BINARY I/O IN JAVA CSC 202 November What should be familiar concepts after this set of topics: All files are binary files. The nature of text files.
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
IO in java.
Exception Handling, Reading and Writing in Files, Serialization,
OO Design and Programming II I/O: Reading and Writing
Ch14 Files and Streams OBJECTIVES
Chapter 8: Input and Output
I/O Basics.
Java Methods ("Chapter %d", 14); Streams and Files A & AB
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Chapter 13: File Input and Output
Java’s Hierarchy Input/Output Classes and Their Purpose
Chapter 17 Binary I/O Dr. Clincy - Lecture.
JAVA IO.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Stream Oriented I/O Computer Programming II Dr. Tim Margush
I/O and Applet from Chapter 12
Files and Streams in Java
Web Design & Development Lecture 8
14 Files and Streams.
CS 240 – Advanced Programming Concepts
David Davenport Spring 2005
Presentation transcript:

Files and Streams The material in this chapter is not tested on the AP CS exams.

Objectives: Learn the basic facts about Java’s IO package Understand the difference between text and binary files Understand the concept of an input or output “stream” Learn a little about exceptions Practice If you want to learn the technical details about Java’s IO package, this is the wrong place. The main objective here is to provide conceptual background on files and streams, with only a glimpse at their implementation in Java. For student projects, EasyReader and EasyWriter are adequate.

Files A file is a collection of data in mass storage. A data file is not a part of a program’s source code. The same file can be read or modified by different programs. The program must be aware of the format of the data in the file. To look at or print out a file you must use some program.

Files (cont’d) The file system is maintained by the operating system. The system provides commands and/or GUI utilities for viewing file directories and for copying, moving, renaming, and deleting files. The system also provides “core” functions, callable from programs, for reading and writing directories and files. Recall that a file is a software entity. The hardware knows nothing about files. The operating system does.

Text Files A computer user distinguishes text (“ASCII”) files and “binary” files. This distinction is based on how you treat the file. A text file is assumed to contain lines of text (e.g., in ASCII code). Each line terminates with a “newline” character (or a combination, carriage return plus line feed). The file name may give a hint as to how it “prefers” to be treated. Surely, a file that contains only ASCII characters and has CR+LF every so often is likely to have been created as an ASCII file and is probably intended to be used that way. But nothing in a file’s contents specifically demands a particular treatment.

Text Files Examples: Any plain-text file, typically named something.txt Source code of programs in any language (e.g., Something.java) HTML documents Data files for certain programs, (e.g., fish.dat; any file is a data file for some program.)

Binary Files A “binary” file contains any information, any combination of bytes. Only a programmer / designer knows how to interpret it. Different programs may interpret the same file differently (e.g., one program displays an image, another extracts an encrypted message). There have been reports in the press of terrorists using software that hides secret encoded messages in harmless-looking image files. There is also the matter of displaying a file. For example, an HTML file is an ASCII file and may be presented as such in a plain-text editor, but it appears differently in a browser.

Binary Files Examples: Compiled programs (e.g., Something.class) Image files (e.g., something.gif) Music files (e.g., something.mp3) Any file can be treated as a binary file (even a text file, if we forget about the special meaning of CR-LF). The reverse is true, too: you can treat a binary file as a text file. Whenever you encounter a byte that contains a value corresponding to LF, the program will decide that this is the end of a line. In the early releases of MS DOS, files contained a special EOF (end of file) marker at the end. The only problem was that a binary file could have this value in the middle by accident, and the copy command would stop at that place. You needed to add a command switch /b (for binary) to tell the program to treat the file as a binary file.

Text as Binary: ASCII display rose.txt A rose is a rose Hex “dump” The hex dump of a text file in this slide has been produced by the ancient program debug (c. 1982, still included with Windows). The display on the right shows those values that correspond to printable ASCII characters. CR + LF

Streams A “stream” is an abstraction derived from sequential input or output devices. An input stream produces a stream of characters; an output stream receives a stream of characters, “one at a time.” Streams apply not just to files, but also to actual IO devices, Internet streams, and so on. The advantage of streams is that they provide a uniform way of treating files and devices. For example, in Unix and other operating systems it is easy to “redirect” screen output to a file.

Streams (cont’d) A file can be treated as an input or output stream. In reality file streams are buffered for efficiency: it is not practical to read or write one character at a time from or to mass storage. It is common to treat text files as streams. For text files it is especially important to be able to treat various data sources (a file, an Internet data stream, etc.) uniformly. It is also more common to read or search text files sequentially, from the beginning.

Random-Access Files A program can start reading or writing a random-access file at any place and read or write any number of bytes at a time. “Random-access file” is an abstraction: any file can be treated as a random-access file. You can open a random-access file both for reading and writing at the same time. Thus you can open the same file as a stream in one situation and as a random-access file in another.

Random-Access Files (cont’d) A binary file containing fixed-length data records is suitable for random-access treatment. A random-access file may be accompanied by an “index” (either in the same or a different file), which tells the address of each record. Tape : CD == Stream : Random-access Random-access is convenient. That’s why people have switched from tapes to CDs and from videotapes to DVDs.

File Types: Summary File Text Binary Stream Random-Access common use possible, but not as common

java.io How do I read an int from a file? RandomAccessFile Reader SequenceInputStream SerializablePermission StreamTokenizer StringBufferInputStream StringReader StringWriter Writer BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter ByteArrayInputStream ByteArrayOutputStream CharArrayReader CharArrayWriter DataInputStream DataOutputStream File FileDescriptor FileInputStream FileOutputStream FilePermission FileReader FileWriter FilterInputStream FilterOutputStream FilterReader FilterWriter InputStream InputStreamReader LineNumberInputStream LineNumberReader ObjectInputStream ObjectInputStream.GetField ObjectOutputStream ObjectOutputStream.PutField ObjectStreamClass ObjectStreamField OutputStream OutputStreamWriter PipedInputStream PipedOutputStream PipedReader PipedWriter PrintStream PrintWriter PushbackInputStream PushbackReader How do I read an int from a file? Java IO is not for the faint at heart. It looks like the developers went all out to confuse programmers. That’s why programmers are such well-paid specialists.