Files CSC 171 FALL 2001 LECTURE 18. History Operating Systems Operating systems (originally called monitors or supervisors) had been developed in the.

Slides:



Advertisements
Similar presentations
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. 2 Operating System Application #1 Application #2 Java Virtual Machine #1 Local Memory Shared Memory Threads.
Advertisements

1 Streams and Input/Output Files Part 3. 2 Handling Primitive Data Types The basic input and output streams provide read/write methods that can be used.
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.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
File input and output. Interactive vs. file I/O All of the programs we have seen or written thus far have assumed interaction with a user, who types in.
File handling in Java. File class The file class is used to store the path and name of a directory or file. The file object can be used to create, rename,
James Tam Simple file handling in Java Simple File Input And Output Types of Java files Simple file output in Java Simple file input in Java.
File Input and Output Recitation 04/03/2009 CS 180 Department of Computer Science, Purdue University.
Chapter 91 Streams and File I/O Chapter 9. 2 Announcements Project 5 due last night Project 6 assigned Exam 2 –Wed., March 21, 7:00 – 8:00 pm, LILY 1105.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
1 CS2200 Software Development Lecture 36: File Processing A. O’Riordan, 2008 (Includes slides by Lewis/Loftus 2205 and K. Brown )
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
Streams CSC 171 FALL 2004 LECTURE 22. Make up exam Friday 12/3 11AM-12:10PM.
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 91 Streams and File I/O CS-180 Recitation-03/07/2008.
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 ->
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 – Files and Streams Outline 17.1 Introduction 17.2 Data Hierarchy 17.3 Files and Streams 17.4.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
Java File I/O (Continued). File I/O in Java Like every other programming language, Java supports the writing to and reading from different files with.
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.
Two Ways to Store Data in a File Text format Binary format.
Introduction Exception handling Exception Handles errors
Introduction Files –Long-term storage of large amounts of data –Persistent data exists after termination of program –Files stored on secondary storage.
1 Chapter 11 – Files and Streams 1 Introduction What are files? –Long-term storage of large amounts of data –Persistent data exists after termination of.
Working with files By the end of this lecture you should be able to: explain the principles of input and output and identify a number of different 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.
Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Text Input and Output on the Console.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
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.
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.
L5: Input & Output COMP206, Geoff Holmes and Bernhard Pfahringer.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
Two Ways to Store Data in a File  Text format  Binary format.
Examples for Project 2 From Deital and Deital, Java: How to Program, Third Edition Reusable and flexible UI Data Records with Sequential and Random Access.
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.
Introduction to Computation and Problem Solving Class 29: Introduction to Streams Prof. Steven R. Lerman and Dr. V. Judson Harward.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
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.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
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
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.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
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.
Using Data Files Eric Roberts CS 106A February 22, 2016.
Recitation File I/O. File I/O File inFile = new File("data.txt"); File inFile = new File (“/Users/sunil/test.txt");
java.io supports console and file I/O
Fundamental of Java Programming
IO in java.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
IO in java.
Chapter 17 Input and Output
Object Writing in files
OBJECT ORIENTED PROGRAMMING II LECTURE 21_1 GEORGE KOUTSOGIANNAKIS
Accessing Files in Java
Streams and File I/O Chapter 9 Chapter 9.
CS 116 Object Oriented Programming II
Chapter 17 - Files and Streams
Chapter 12 File Input and Output
Chapter 17 – Files and Streams
OBJECT ORIENTED PROGRAMMING II LECTURE 11_1 GEORGE KOUTSOGIANNAKIS
The Package Statement Group related interfaces and classes together
David Davenport Spring 2005
Presentation transcript:

Files CSC 171 FALL 2001 LECTURE 18

History Operating Systems Operating systems (originally called monitors or supervisors) had been developed in the late 1950s Users were frustrated by their lack of intimacy with the computer Fernando Corbató, MIT, produced CTSS (Compatible Time Sharing System) for the IBM 7090/94, the first effective time-sharing system and coincidentally the first practical means of remote access.

History By the mid-1960's the jet airliner (and Boeing) had revolutionized the airline travel business but the reservation process was inadequate. After remote access had been proven by the CTSS system, IBM produced the first large scale, on-line, real-time reservation tracking system, named SABRE for American Airlines, and soon to be copied by others.

History III 1962 – The first video game (Spacewar) is invented by MIT grad student Steve Russel

File IO We have seen character I/O Sometimes, we want to have other data types stored in file – It’s all just bits

Consider saving an array a = new float[5][5]; for(int i = 0 ; i<a.length;i++) for(int j = 0 ; j<a[i].length;j++) a[i][j] = ((float)i+1) / ((float)j+1) ;

Open the File private void saveArray() { try { JFileChooser chooser = new JFileChooser(); PrintWriter out1 = null; if (chooser.showOpenDialog(null)== JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); FileWriter writer = new FileWriter(selectedFile); out1 = new PrintWriter(writer);

Write the array for(int i = 0 ; i<a.length;i++) for(int j = 0 ; j<a[i].length;j++){ out1.println(a[i][j] + " "); System.out.println(" " + a[i][j]); } writer.close(); } } catch (IOException e) {System.out.println("problem");} }

Open the file private void restoreArray() { try { JFileChooser chooser = new JFileChooser(); BufferedReader in1 = null; if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); FileReader reader = new FileReader(selectedFile); in1 = new BufferedReader(reader);

Read the array for(int i = 0 ; i<a.length;i++) for(int j = 0 ; j<a[i].length;j++){ String s1 = in1.readLine(); System.out.println(s1); a[i][j] = (float) Double.parseDouble(s1); } reader.close(); } } catch (IOException e) {System.out.println("problem");} repaint(); }

Text File

Byte by Byte Text file 303 bytes \r \n \r \n \r \n \r \n 0. 2 \r \n \r \n \r \n \r \n 0. 5 \r \n 0. 4 \r \n \r \n \r \n 1. 0 \r \n \r \n \r \n 4. 0 \r \n \r \n \r \n \r \n 0. 8 \r \n 5. 0 \r \n \r \n \r \n \r \n 1. 0 \r \n

Open the file (binary) private void saveArray2() { try { JFileChooser chooser = new JFileChooser(); DataOutputStream out1 = null; if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); FileOutputStream writer = new FileOutputStream(selectedFile); out1 = new DataOutputStream(writer);

Write the file for(int i = 0 ; i<a.length;i++) for(int j = 0 ; j<a[i].length;j++){ out1.writeFloat(a[i][j]); System.out.println(" " + a[i][j]); } writer.close(); } } catch (IOException e) {System.out.println("problem");} }

Open the file private void restoreArray2() { try { JFileChooser chooser = new JFileChooser(); DataInputStream in1 = null; if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); FileInputStream reader = new FileInputStream(selectedFile); in1 = new DataInputStream(reader);

Read the file for(int i = 0 ; i<a.length;i++) for(int j = 0 ; j<a[i].length;j++){ a[i][j] = in1.readFloat(); } reader.close(); } } catch (IOException e) {System.out.println("problem");} repaint(); }

Same data Bytes e e e e e e e e e e e e e e e e e e e e e e e e e+00

Data Streams close() readBoolean() readByte() readChar() readDouble() readFloat() readInt() readLong() readShort() readLine() flush() close() writeByte(int b) writeBoolean(boolean v) writeBytes(String s) writeChar(int v) writeChars(String s) writeDouble(Double v) writeFloat(Float v) writeInt(int v) writeLong(long v) writeShort(int v)

Serializable Objects We can write whole objects to files

public AccountRecord( int acct, String first, String last, double bal ) { setAccount( acct ); setFirstName( first ); setLastName( last ); setBalance( bal ); } public void setAccount( int acct ) { account = acct; } public int getAccount() { return account; } public void setFirstName( String first ) { firstName = first; } public String getFirstName() { return firstName; } public void setLastName( String last ) { lastName = last; } public String getLastName() { return lastName; } public void setBalance( double bal ) { balance = bal; } public double getBalance() { return balance; } public String toString(){ return "Account Record " + " " + account + " " + firstName + " " + lastName + " " + balance; }

// Java core packages import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CreateSequentialFile extends JFrame{ private ObjectOutputStream output; public CreateSequentialFile(){ AccountRecord record; openFile(); try { // create new record record = new AccountRecord( 1, "Ted","Pawlicki",100); System.out.println(record.toString()); output.writeObject( record ); output.flush(); // create new record record = new AccountRecord( 2, "Adam","Frank",2000); System.out.println(record.toString()); output.writeObject( record ); output.flush();

// allow user to specify file name private void openFile() { // display file dialog, so user can choose file to open JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); int result = fileChooser.showSaveDialog(this); // get selected file File fileName = fileChooser.getSelectedFile(); // display error if invalid if ( fileName == null || fileName.getName().equals( "" ) ){ System.err.println( "Invalid File Name"); return; } else {// open file try { output = new ObjectOutputStream( new FileOutputStream( fileName ) ); } // process exceptions from opening file catch ( IOException ioException ) { System.err.println( "Error Opening File"); return; } } // end method openFile

// close file and terminate application private void closeFile() { // close file try { output.close(); System.exit( 0 ); } // process exceptions from closing file catch( IOException ioException ) { System.err.println( "Error closing file"); System.exit( 1 ); } public static void main( String args[] ) { new CreateSequentialFile(); }

public ReadSequentialFile(){ AccountRecord record; openFile(); try { // create new record record = (AccountRecord) input.readObject(); System.out.println(record.toString()); record = (AccountRecord) input.readObject(); System.out.println(record.toString()); record = (AccountRecord) input.readObject(); System.out.println(record.toString()); } catch(ClassNotFoundException e) { System.err.println(" Problem with class "); System.exit(1); } catch(IOException e) { System.err.println(" Problem with file "); System.exit(1); } closeFile(); }

\0 005 s r \0 \r A c c o u n t R e c o r d G \r 002 \ I \0 007 a c c o u n t D \0 007 b a l a n c e L \0 \t f i r s t N a m e t \0 022 L j a v a / l a n g / S t r i n g ; L \0 \b l a s t N a m e q \0 ~ \0 001 x p \0 \0 \0 Y \0 \0 \ \0 \0 \0 t \0 003 T e d t \0 \b P a w l i c k i s q \0 ~ \0 \0 \0 \0 \0 237 \0 \0 \0 \0 \0 t \0 004 A d a m t \ F r a n k s q \0 ~ \0 \0 \0 \0 \0 003 A c \0 \0 \0 \0 t \0 003 J o e t \ S c h m o