Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Files.

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

Click to edit Master title style. Click to edit Master subtitle style.
Files. Advantages of files Can edit data, prepare ahead of time Can rerun file without reentering data Can examine output at leisure Can display output.
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.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
CPSC150 Week 13 Chapter 12 Exceptions (from slides provided by textbook web site)
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
CPSC150 JavaLynn Lambert Week 13 Rest of Guis (from Bluej web page) mainExceptions.
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.
1 Streams 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 Writing.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Java Review 2. The Agenda The following topics were highlighted to me as issues: –File IO (Rem) –Wrappers (Rem) –Interfaces (Rem) –Asymptotic Notation.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Files Section 2.13, Section 8.8.
2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
Handling errors Exception handling and throwing Simple file processing.
CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)
Two Ways to Store Data in a File Text format Binary format.
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,
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,
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.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
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.
Files. Advantages of files Can edit data, prepare ahead of time Can rerun file without reentering data Can examine output at leisure Can display output.
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 BUILDING JAVA PROGRAMS CHAPTER 6 DETAILS OF TOKEN-BASED PROCESSING.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
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.
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
CS1020 Data Structures and Algorithms I Lecture Note #16 File Processing.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to programming in java
For Friday Finish reading chapter 9 WebCT quiz 17.
Headline sample style Intro sample style Click to edit Master text styles –Second level Third level –Fourth level o Fifth level.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
OO Design and Programming II I/O: Reading and Writing
File Input / Output.
Streams & File Input/Output (I/O)
Introduction to programming in java
Click to Add Title Click to Add Subtitle.
Strings and File I/O.
Click to Add Title Click to Add Subtitle.
Testing and Exceptions
הרצאה 12: קבצים וחריגות (Exceptions)
COS 260 DAY 27 Tony Gauvin.
Click to edit Master text styles
שימוש במחלקות קיימות מחרוזות, קבצים, וקבלת קלט מהמשתמש
Reading and Writing Text Files
Click to Add Title Click to Add Subtitle.
Click to Add Title Click to Add Subtitle.
Author names here Author association names here
Click to edit Master text styles
Unit 6 Working with files. Unit 6 Working with files.
TITLE SLIDE GOES HERE Optional subhead.
Click to edit Master text styles
Slide Title Edit Master text styles Second level Third level
ОПШТЕСТВО ТЕМА: МЕСТОТО ВО КОЕ ЖИВЕАМ Скопје
Author names here Author associations here
Author names here Author associations here
Click to edit Master text styles
CSC1401 Input and Output (with Files)
Author names here Author associations here
Click to edit Master text styles
Presentation transcript:

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Files

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 2 Files To use files, use File class File class can’t read/write To read/write files, use Reader/Writer classes File input can be text-based (what we’ll do), stream-based

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 3 Readers, writers, streams Readers and writers deal with textual input. –Based around the char type. Streams deal with binary data. –Based around the byte type.

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 4 Text output Use the FileWriter class. –Tie File object to diskfile name –Open a FileWriter; tie to File. –Write to the FileWriter. –Close the FileWriter. Failure at any point results in an IOException.

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 5 Text output try { FileWriter writer = new FileWriter("name of file"); while(there is more text to write) {... writer.write(next piece of text);... } writer.close(); } catch(IOException e) { something went wrong with accessing the file }

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 6 // file output. public void writeto(String fileout, int nbrlines) throws IOException // have throws or throw { File outfile = new File(fileout); FileWriter fw = new FileWriter(outfile); fw.write("There were " + nbrlines + “ lines in the input file."); fw.close(); }

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 7 import java.io.*; public class countlines { // all methods that do something inserted here public static void main(String[] args) { if (args.length != 2) { System.out.println("to run, type: 'countlines filein fileout'"); System.exit(1); } // create a new countline object with first arg as input file, second as output countlines cl = new countlines(args[0], args[1]); }

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 8 Write a program to count number of lines in a file and write the number to an output file java countlines myinfile.txt myoutfile.txt would return 2 if file is: This is a file with two lines. myinfile.txt

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 9 // constructor public countlines(String filein, String fileout) { try { int nbrLines = readfrom(filein); writeto(fileout, nbrLines); } catch (IOException e) { // will catch IO exception not caught by readfrom and writeto System.out.println("Problems with IO. " + " Program aborted without successful write."); }

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 10 Scanner Class: for reading files public void readfrom(String filein) { Scanner scanner; try { scanner = new Scanner(new File(filein)); String line = scanner.nextLine( ); // also next( ), nextInt( ), nextDouble( ) while (line != null) { System.out.println(line); // for debugging only; countlines++; } catch (FileNotFoundException ex) { System.out.println("File not found...aborting program."); System.exit(1); } }

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 11 Console input Scanner class: Java 5 import java.util.*; class MyScanner { public MyScanner ( ) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number: "); int x = scanner.nextInt(); int y = scanner.nextInt(); System.out.println("The sum of " + x + " + " + y + " is " + (x+y)); }

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 12 File input using Scanner import java.util.*; import java.io.*; class MyScanner { public MyScanner ( ) { Scanner scanner; try { scanner = new Scanner(new File("infile.java")); int x = scanner.nextInt( ); int y = scanner.nextInt( ); System.out.println("The sum of " + x + " + " + y + " is " + (x+y)); } catch (FileNotFoundException ex) { System.out.println("File not found...aborting program."); System.exit(1); } }