CSC 213 – Large Scale Programming. Project #1 Recap.

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 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.
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
Random Access Files Sandeep Patel. Random Access Files Random access files are files in which records can be accessed in any order –Also called direct.
1 File Output. 2 So far… So far, all of our output has been to System.out  using print(), println(), or printf() All input has been from System.in 
Slides prepared by Rose Williams, Binghamton University Chapter 10 File 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.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
CSC – Java Programming II Lecture 9 January 30, 2002.
CS0007: Introduction to Computer Programming File IO and Recursion.
1 Chapter 15 – Files I/O basics File I/O Classes File I/O Basic Operations Text File Output PrintWriter import Statement with a * Text File Input Scanner,
Dynamic Arrays Dynamic arrays are arrays that are re- allocated to a larger size. –See Eck Section 8.3 for details. –For instance, what happens with the.
Input / Output Chapter 13.  We use files all the time  Programs are files  Documents are files  We want them to be “permanent”  To last beyond execution.
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.
CSC 213 – Large Scale Programming. What is “the BTree?”  Common multi-way tree implementation  Every BTree has an order (“BTree of order m ”) ‏  m.
Chapter 15 – Files I/O basics File I/O Classes File I/O Basic Operations Text File Output PrintWriter import Statement with a * Text File Input Scanner,
Files and Streams. Midterm exam Time: Wednesday, October 31, 2007 Format: Multiple choices (about 15 to 20 questions) Determine the results of the code.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Working with files. RHS – SOC 2 Motivation All our programs so far have only worked with data stored in primary storage (RAM) Data is lost when program.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
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.
CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Chapter 9-Text File I/O. Overview n Text File I/O and Streams n Writing to a file. n Reading from a file. n Parsing and tokenizing. n Random Access n.
CSC 213 Lecture 10: BTrees. Announcements You should not need to do more than the lab exercise states  If only says add a CharRange, you should not need.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
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.
Lecture 5 I/O and Parsing
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
CSC 213 – Large Scale Programming Lecture 38: BTrees.
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.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Chapter 15: Input and Output
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1.
CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.
Java Input/Output. Java Input/output Input is any information that is needed by your program to complete its execution. Output is any information that.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 (3 weeks)
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.
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
CS 116 Object Oriented Programming II Lecture 11 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
PHY 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.
Java IO Exploring the java.io package and living to talk about it.
Fundamental of Java Programming
IO in java.
IO in java.
Chapter 17 Input and Output
Introduction to programming in java
Object Writing in files
OBJECT ORIENTED PROGRAMMING II LECTURE 21_1 GEORGE KOUTSOGIANNAKIS
I/O Basics.
Chapter 17 Binary I/O 1.
Computer Programming Methodology File Input
CS 116 Object Oriented Programming II
Streams and File I/O Chapter 14.
Fundamentals of Data Structures
OBJECT ORIENTED PROGRAMMING II LECTURE 11_1 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
The Package Statement Group related interfaces and classes together
David Davenport Spring 2005
Presentation transcript:

CSC 213 – Large Scale Programming

Project #1 Recap

Today's Goals

 Discuss reasons why files & file I/O important  When & where used and what real value does it offer?  Show how to read & write text to files in Java  Classes & methods needed to perform these actions  How these methods move through file as they work  Limits of these actions & why we might want more  Discuss another approach: RandomAccessFile s  Benefits of using this for reading & writing data  How this also has additional ways to access data

Image To Sharpen  I have a (fuzzy) 1024 x 768 picture to sharpen 786,432  Only 786,432 numbers to type into photo application  After analysis, must click & update each pixel

Image To Sharpen  I have a (fuzzy) 1024 x 768 picture to sharpen 786,432  Only 786,432 numbers to type into photo application  After analysis, must click & update each pixel

More Data Entry Positions  Testing improved jet designs for oeingB-ay  Using program to simulate designs' lift & drag  5 possible designs (each 150MB) to test this iteration  Once results available, will tweak & retest designs  Need room of touch typists for all this data entry

This Is (Semi-Real) Problem  Large hadron collider about to come on-line  No black hole when smashing particles at high speeds 28.5 GB/min  Creates 28.5 GB/min for nerds seeking truth & beauty

This Is (Semi-Real) Problem  Large hadron collider about to come on-line  No black hole when smashing particles at high speeds 28.5 GB/min  Creates 28.5 GB/min for nerds seeking truth & beauty

This Is (Semi-Real) Problem  Large hadron collider about to come on-line  No black hole when smashing particles at high speeds 28.5 GB/min  Creates 28.5 GB/min for nerds seeking truth & beauty  Hired trained monkeys to type data into programs

This Is (Semi-Real) Problem  Large hadron collider about to come on-line  No black hole when smashing particles at high speeds 28.5 GB/min  Creates 28.5 GB/min for nerds seeking truth & beauty  Hired trained monkeys to type data into programs college students

This Is (Semi-Real) Problem  Large hadron collider about to come on-line  No black hole when smashing particles at high speeds 28.5 GB/min  Creates 28.5 GB/min for nerds seeking truth & beauty  Hired trained monkeys to type data into programs college students

Yeah, Right

 Real world demands we use files for most I/O  Data files used to start and/or end most projects  May contain: game levels, analysis results, CCD pics  Way to read & write files needed to be useful

Reading a Text File  Must first instantiate java.io.File object  Pass String filename to the File constructor  Throws a (checked) exception if file does not exist  Another IOException possible for other odd errors  Once created, use File to create Scanner  Reads file's data rather than typing into keyboard  At the same time, works like any other Scanner

Reading a Text File try { File readFile = new File("bob.dat"); Scanner scan = new Scanner(readFile); while (scan.hasNext()) { String line = scan.nextLine(); System.out.println(line); } scan.close(); } catch (FileNotFoundException fnfe) { System.err.println("Make the file, moron!"); } catch (IOException ioe) { ioe.printStackTrace(); }

Typical File I/O  Ordinarily we read files sequentially Scanner scan ; // Instantiate a Scanner scan for the “file” below char c = ‘’; while (c != ‘s’) { c = scan.nextChar(); } scan Are 10^15 Files Just a Peta-File?

Typical File I/O  Ordinarily we read files sequentially Scanner scan ; // Instantiate a Scanner scan for the “file” below char c = ‘’; while (c != ‘s’) { c = scan.nextChar(); } scan Are 10^15 Files Just a Peta-File?

Typical File I/O  Ordinarily we read files sequentially Scanner scan ; // Instantiate a Scanner scan for the “file” below char c = ‘’; while (c != ‘s’) { c = scan.nextChar(); } scan Are 10^15 Files Just a Peta-File?

Typical File I/O  Ordinarily we read files sequentially Scanner scan ; // Instantiate a Scanner scan for the “file” below char c = ‘’; while (c != ‘s’) { c = scan.nextChar(); } scan Are 10^15 Files Just a Peta-File?

Typical File I/O  Ordinarily we read files sequentially Scanner scan ; // Instantiate a Scanner scan for the “file” below char c = ‘’; while (c != ‘s’) { c = scan.nextChar(); } scan Are 10^15 Files Just a Peta-File?

Writing a Text File  Writing a text file only slightly more complicated  Console is file in Unix, so can guess where this goes  Need to first decide what should happen to file  Easy if file does not exist  create file & write to it  Else what should happen to file's current contents?  Mode used at opening determines file's contents  If opening file in write mode, erases file at the start  Starts at end of file in append mode, saving the data

Opening File For Writing  Create instance of java.io.FileWriter  Must specify mode to open file at this time  Be very careful with this – there is no undo here!  If file is impossible and so cannot be written to  Cannot be done, so system throws IOException  Not told if file existed before this command FileWriter nuked=new FileWriter("boom.t", false); FileWriter saved = new FileWriter("ScoreOnRebound",true);

Second Step To Writing Files  FileWriter helps, but slow and hard to use  Faster, simpler approach would be much nicer  Using FileWriter create BufferedWriter  Cannot change mode; must take care initially  Two methods used to write out data to file  Both methods will expand file & advance pointer  Start writing new line – newLine()  write(String s) – writes s to file  End writing & save results with close()

Writing a Text File try { FileWriter fw = new FileWriter(“b.t”, true); BufferedWriter bw = new BufferedWriter(fw); for (int i = 10; i > 0; i--) { bw.write(“T minus ”); bw.write(i + “”); bw.newLine(); } bw.write(“Blast off!”); bw.close(); } catch (IOException ioe) { ioe.printStackTrace(); }

Its Not All Text  We often want to store more than just text  Translate numbers into binary to be used in program  Storing as text wastes time converting back & forth  (Often) Space also wasted for larger numbers  Could instead store numbers in binary format  Optimized for machine, as not easily human-readable  But how often do we look at numbers in image file?  Easy to determine sizes; each type has specific length  To enable binary formats, use different File class

RandomAccessFile  Built into Java's standard set of classes  Found in the java.io package  New or existing files can be accessed with it RandomAccessFile raf = new RandomAccessFile("f.txt","rw");  First argument ( "f.txt" ) is name of file used  Access to file specified ( "rw" ) in second parameter  Using write access ( "w" ) erases any data in the file  Read & write anywhere in file using instance

Reading RandomAccessFile  Defines methods to read most primitive types: boolean readBoolean() int readInt() double readDouble()  Reads & returns value read from file  Binary encoding used automatically  File will store 32-bit int, not "125"  Not human readable, but not really needed  Can shrink files; always makes sizes predictable

Reading RandomAccessFile  Reading String s takes a little extra work String readUTF()  Requires that String was recorded in UTF format  Not totally readable, but makes sense to machines  Or use readChar() to read in String …  …but need null character (' \0 ') at end  End of String not easy to find without some hint  Also remember that Java’s char not always readable  readByte() is readable, but needs typecast

Writing RandomAccessFile  Also defines methods to write to a file: void writeInt(int i) void writeDouble(double d) void writeUTF(String s)  Writes value at location in the file we are currently at  As it is needed, methods extend file also  When writing data, erases anything there previously

RandomAccessFile I/O  Unless specified still read &write sequentially RandomAccessFile raf = new …; char c = ‘’; while (c != ‘s’) { c = (char)raf.readByte(); raf.writeByte((byte)c); } Could I rename machine "PetaHertz"

Skipping Around The File  RandomAccessFile allows moving in the file  Skip past sections using int skipBytes(int n)  void seek(long pos) moves to position in file  Positions specified as bytes from beginning of file

RandomAccessFile I/O  Sequential access is no longer required RandomAccessFile raf = new …; char c; raf.skipBytes(raf.length()-1); c = (char)raf.readByte(); raf.seek(0); raf.writeByte((byte)c); Could I rename machine "PetaHertz"

RandomAccessFile I/O  Sequential access is no longer required RandomAccessFile raf = new …; char c; raf.skipBytes(raf.length()-1); c = (char)raf.readByte(); raf.seek(0); raf.writeByte((byte)c); Could I rename machine "PetaHertz"

RandomAccessFile I/O  Sequential access is no longer required RandomAccessFile raf = new …; char c; raf.skipBytes(raf.length()-1); c = (char)raf.readByte(); raf.seek(0); raf.writeByte((byte)c); Could I rename machine "PetaHertz"

RandomAccessFile I/O  Sequential access is no longer required RandomAccessFile raf = new …; char c; raf.skipBytes(raf.length()-1); c = (char)raf.readByte(); raf.seek(0); raf.writeByte((byte)c); Could I rename machine "PetaHertz"

RandomAccessFile I/O  Sequential access is no longer required RandomAccessFile raf = new …; char c; raf.skipBytes(raf.length()-1); c = (char)raf.readByte(); raf.seek(0); raf.writeByte((byte)c); "ould I rename machine "PetaHertz"

For Next Lecture