java.io supports console and file I/O

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
The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
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.
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.
Java I/O and Java Networking (Client Side) Yoshi.
Class Decimal Format ► Import package java.text ► Create DecimalFormat object and initialize ► Use method format ► Example: import java.text.DecimalFormat.
Streams CSC 171 FALL 2004 LECTURE 22. Make up exam Friday 12/3 11AM-12:10PM.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
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.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
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/
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.
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.
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
1 Streams Files are a computer’s long term memory Need ability for programs to –get information from files –copy information from program variables to.
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,
Copyright © Curt Hill Java I/O Flexibility and Layering.
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 Dennis Burford
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java.
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.
Two Ways to Store Data in a File  Text format  Binary format.
CHAPTER 15 STREAMS. CHAPTER GOALS To be able to read and write files To become familiar with the concepts of text and binary files To be able to read.
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.
– 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.
Files and console applications Chapter 17 This chapter explains: What a file is How to read and write to files How to manipulate folder paths and file.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
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.
The Java IO System Different kinds of IO Different kinds of operations
CSC1351 Class 6 Classes & Inheritance.
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.
Part I General Principles
OO Design and Programming II I/O: Reading and Writing
Ch14 Files and Streams OBJECTIVES
I/O Basics.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Java’s Hierarchy Input/Output Classes and Their Purpose
Chapter 12 File Input and Output
JAVA IO.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Input and Output Stream
I/O and Applet from Chapter 12
Files and Streams in Java
Web Design & Development Lecture 8
David Davenport Spring 2005
Presentation transcript:

java.io supports console and file I/O most classes work with I/O streams (have beginning, end, and are read sequentially) Random access is supplied by RandomAccessFile class output formatting is provided by java.text classes byte stream classes - raw data character stream classes - do not assume that a character is one byte Object stream classes

Stream I/O byte stream classes - raw data character stream classes - do not assume that a character is one byte Object stream classes

Byte Streams ByteArrayOutputStream OutputStream FileOutputStream Abstract superclasses ByteArrayOutputStream OutputStream FileOutputStream FilterOutputStream ByteArrayInputStream FileInputStream InputStream FilterInputStream * These subclasses may be superclasses themselves

Input Byte Streams InputStream FilterInputStream BufferedInputStream FilterInputStream acts as a ‘wrapper’ for an InputStream object, which is uses as its basic source of data. Methods of FilterInputStream simply pass requests to the InputStream objetct. A subclass of FilterInputStream, transforms the data along the way, or provides some additional functionality. For example, BufferedInputStream ‘wraps’ a InputStream and provides ‘buffering’ for efficiency, so that file access is not made for byte access. BufferedInputStream overloads ‘read’ so that in addition to one byte at a time, one byte array can be read. System.in, is an object of type BufferedInputStream, which is provided by the System class in the java.lang input. System.in ‘wraps’ the InputStream object representins the keyboard.

Byte Console Input int next = System.in.read() ; char c; if (next != -1) c = (char)next; //read() reads one byte at a time, // returning it as an int //(overriding InputStream method

Output Byte Streams OutputStream FilterOutputStream PrintStream FilterOutputStream acts as a ‘wrapper’ for an OutputStream object, which it uses as its basic depository of data. Methods of FilterOutputStream simply pass requests to the OutputStream objetct. A subclass of FilterOutputStream, transforms the data along the way, or provides some additional functionality. For example, PrintStream ‘wraps’ a OutputStream and provides an accurate display of data types to the output, in addition to buffering. PrintStream also provides the overloaded println, which accepts String and byte[] parameters, as well as byte. System.out, is an object of type PrintStream, which is provided by the System class in the java.lang input. System.out ‘wraps’ the OutputStream object representins the console.

Byte Console Output int next = System.in.read() ; System.out.write(next); System.out.flush(); System.out.println(“hello world”); //println is an overloaded method // which handles the display of // it’s parameter data according // to data type

File I/O (Byte) Programming File I/O is much like programming stream I/O to and from the console -- once a stream is established, its usage is the same regardless of the source/destination!! However, a file must be associated with a FileInputStream or FileOutputStream object … FileInputStream in = new FileInputStream (“in.txt”); BufferedInputStream bufIn = new BufferedInputStream(in); int bb = bufIn.read();

File I/O (Byte) A FileInputStream can be obtained in another manner… However, a file must be associated with a FileInputStream or FileOutputStream object … File f = new File(“in.txt”); FileInputStream in = new FileInputStream(f); BufferedInputStream bufIn = new BufferedInputStream(in); int bb = bufIn.read(); The file object will allow you to check the status of a file before you open it!!

File f = new File(“in.txt”); if ( ! f.exists() ) System.out.println(“file does not exist”); else if ( ! F.canRead() ) System.out.println(“file cannot be read”); else { FileInputStream in = new FileInputStream(f); BufferedInputStream bufIn = new BufferedInputStream(in); int bb = bufIn.read(); … etc. ………

Some more File methods delete renameTo

character stream classes - do not assume that a character is one byte Obviously, we don’t always want to use byte I/O. Java classes InputStreamReader and OutputStreamReader provide the ‘character’ I/O. InputStreamReader x = new InputStreamReader( character stream classes - do not assume that a character is one byte Object stream classes

Character Streams BufferedWriter Writer OutputStreamWriter PrintWriter Abstract superclasses BufferedWriter Writer OutputStreamWriter PrintWriter BufferedReader Reader InputStreamReader Obviously, we don’t always want to use byte I/O. Java classes InputStreamReader and OutputStreamReader provide the ‘character’ I/O.

InputStreamReader reader = new InputStreamReader(System.in); //InputStreamReader constructor parameter is InputStream InputStreamReader reader = new InputStreamReader(System.in); // now have one ‘character’ at a time //where character is defined //by the current InputStreamReader charset char cc = (char) in.read(); But we might not want to read one char at a time… BufferedReader is a class which also extends Reader…. public BufferedReader(Reader)

InputStreamReader reader = new InputStreamReader(System.in); // now have one ‘character’ at a time //where character is defined //by the current InputStreamReader charset BufferedReader in = BufferedReader(reader); //wrap in another class //now have buffered input AND a readLine // method String inputLine = in.readLine(); double x = Double.parseDouble(inputLine);

Character Stream Input Reader InputStreamReader FileReader The FileReader class extends InputStreamReader.. and so it IS an InputStreamReader

Same concepts apply to file FileReader reader = new FileReader("input.txt"); int next = reader.read() ; if (next != -1) char c = (char)next(); ... reader.close()

A filereader can also be wrapped in a BufferedReader object.. FileReader reader = new FileReader ("input.txt"); BufferedReader in = new BufferedReader(reader); String inputLine = in.readLine(); double x = Double.parseDouble(inputLine);

Character Stream Input Writer InputStreamWriter FileWriter The FileReader class extends InputStreamReader.. and so it IS an InputStreamReader

Code to Write a Character to Disk FileWriter writer = new FileWriter("output.txt"); ... char c=''; ... writer.write(c); ... write.close();

PrintWriter class has println FileWriter writer = new FileWriter(“output.txt”) PrintWriter out = new PrintWriter(writer); out.println(“goodbye”);

File Dialogs Use JFileChooser to let a user supply a file name through a file dialog Construct a file chooser object Call its showOpenDialog or showSaveDialog method (Specify null or the user interface component over which to pop up the dialog )

If the user chooses a file: JFileChooser.APPROVE_OPTION is returned File Dialogs If the user chooses a file: JFileChooser.APPROVE_OPTION is returned If the user cancels the selection: JFileChooser.CANCEL_OPTION is returned If a file is chosen, use GetSelectedFile method to obtain a File object describing the file

Code to Use a JFileChooser JFileChooser chooser new JFileChooser(); FileReader in; if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); in = new FileReader(selectedFile); } else in = null;

A JFileChooser Dialog