Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

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.
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.
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.
0 - 0.
Addition Facts
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
Test on Input, Output, Processing, & Storage Devices
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
Lecture 15: I/O and Parsing
Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Addition 1’s to 20.
Mr. Wortzman.  So far, we have gotten all our input and written all our output to the console  In reality, this is somewhat uncommon  Instead, we often.
Test B, 100 Subtraction Facts
Week 1.
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.
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.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
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.
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
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.
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.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
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.
Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.
Java Review 2. The Agenda The following topics were highlighted to me as issues: –File IO (Rem) –Wrappers (Rem) –Interfaces (Rem) –Asymptotic Notation.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
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.
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,
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
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 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.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 11.
Chapter 9 1 Chapter 9 – Part 2 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.
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.
File Input and Output Appendix E © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
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.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
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.
For Friday Finish reading chapter 9 WebCT quiz 17.
Basic Text File Input/Output
File - CIS 1068 Program Design and Abstraction
Text File Input/Output
Streams & File Input/Output (I/O)
CMSC 202 Text File I/O.
Introduction to programming in java
Streams and File I/O.
Text File Input/Output
I/O Basics.
Some File I/O CS140: Introduction to Computing 1 Savitch Chapter 9.1, 10.1, /25/13.
CSS161: Fundamentals of Computing
Streams and File I/O Chapter 9 Chapter 9.
Streams and File I/O Chapter 14.
JAVA IO.
CSS 161: Fundamentals of Computing
File I/O ICS 111: Introduction to Computer Science I
Podcast Ch23c Title: Binary Files
David Davenport Spring 2005
Streams A stream is an object that enables the flow of data between a program and some I/O device or file If the data flows into a program, then the stream.
Presentation transcript:

Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1

Chapter File IO Overview 10.2 Text-File IO 10.3 File Techniques 10.6 Graphics Supplement 2

10.1 File IO Overview 3

A stream is a stream is a stream All IO can be viewed as a stream of data 4

An Input stream delivers data to your program Streaming data 5

An Output stream accepts data from your program Streaming data 6

A file could be treated as text or binary All data is binary at the end of the day 7

A text file treats the binary data as characters Each digit uses 1 (ASCII) or 2 (Unicode) bytes 8

A binary file manipulates the binary data Each data item uses the same number of bytes (4 for ints) 9

Recap  A program reads data from an input stream  A program writes data to an output stream  A text file interprets its binary data as text  A binary file deals with the binary data itself 10

10.2 Text-File IO 11

There are four steps in using file IO Open the file stream 1 Test the Connection 2 Perform the IO 3 Close the stream 4 12

(1) Open the file stream // Reading Scanner fin = null; try { fin = new Scanner(new File(filename)); } // Writing PrintWriter fout = null; try { // Create empty file fout = new PrintWriter(filename); // or fout = new PrintWriter(new FileOutputStream(filename, false)); // Append to the file fapp = new PrintWriter(new FileOutputStream(filename, true)); } 13

(2) Test the Connection catch (FileNotFoundException e) { System.err.println("Error opening file " + filename); System.exit(1); } 14

(3) Perform the IO // Reading while (fin.hasNextLine()) { String line = fin.nextLine(); // process the line... } // Writing fout.println("Write some output");... 15

(4) Close the Stream // Reading fin.close(); // Writing fout.close(); 16

Application Deconstructed package fileoutput; import java.io.PrintWriter; import java.io.FileNotFoundException; public class FileOutput { public static void main(String[] args) { PrintWriter fout = null; String filename = "fileOutput.txt"; // Open and test the output stream. try { fout = new PrintWriter(filename); } catch (FileNotFoundException e) { System.err.println("Error opening the file " + filename); System.exit(1); }// end try 17

Application Deconstructed // Perform some IO. for (int line = 1; line <= 5; line++) { fout.println(line + ": " + "This is just a line"); } // Close the stream. fout.close(); System.out.println("File " + filename + " was created successfully."); }// end main() } // end FileOutput 18

Application Deconstructed 19

Application Deconstructed package fileinput; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class FileInput { public static void main(String[] args) { String filename = "fileOutput.txt"; Scanner fin = null; // Open and test the stream. try { fin = new Scanner(new File(filename)); } catch (FileNotFoundException e) { System.err.println(e); System.exit(1); }// end try 20

Application Deconstructed // Perform the IO. while (fin.hasNext()) { int lineNumber = Integer.parseInt( fin.next().replace(":", "")); String lineText = fin.nextLine(); System.out.println(lineText + lineNumber); }// end while // Close the stream. fin.close(); }// end main() }// end FileInput 21

Recap  Open the file stream first in a try block  Catch any errors that occur during opening  Perform the IO  Close the stream as soon as IO is finished 22

10.3 File Techniques 23

The File class offers useful functionality public boolean canRead()// Can program read from the file? public boolean canWrite()// Can program write to the file? public boolean delete()// Was file detected? public boolean exists()// Does the file exist? String getName()// Get file's name. String getPath()// Get file's path. public long length()// Get file's path. 24

Application Deconstructed package fileclass; import java.io.File; import java.util.Scanner; public class FileClass { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); File file = null; // Ask the user for the file. System.out.print("Enter the filename: "); file = new File(keyboard.nextLine()); 25

Application Deconstructed // Let's test some File methods. System.out.println(""); System.out.println("Does file exist? " + String.valueOf(file.exists()));... }// end main() }// end FileClass 26

Application Deconstructed // Let's test some File methods.... System.out.println("Can file be read? " + String.valueOf(file.canRead()));... }// end main() }// end FileClass 27

Application Deconstructed // Let's test some File methods.... System.out.println("Can file be written? " + String.valueOf(file.canWrite()));... }// end main() }// end FileClass 28

Application Deconstructed // Let's test some File methods.... System.out.println("Filename: " + file.getName());... }// end main() }// end FileClass 29

Application Deconstructed // Let's test some File methods.... System.out.println("File path: " + file.getPath());... }// end main() }// end FileClass 30

Application Deconstructed // Let's test some File methods.... System.out.println("File absolute path: " + file.getAbsolutePath());... }// end main() }// end FileClass 31

Application Deconstructed // Let's test some File methods.... System.out.println("File length: " + file.length() + " bytes"); }// end main() }// end FileClass 32

Application Deconstructed // Let's test some File methods. System.out.println(""); System.out.println("Does file exist? " + String.valueOf(file.exists())); System.out.println("Can file be read? " + String.valueOf(file.canRead())); System.out.println("Can file be written? " + String.valueOf(file.canWrite())); System.out.println("Filename: " + file.getName()); System.out.println("File path: " + file.getPath()); System.out.println("File absolute path: " + file.getAbsolutePath()); System.out.println("File length: " + file.length() + " bytes"); }// end main() }// end FileClass 33

Application Deconstructed 34

Application Deconstructed package csvfile; import java.io.PrintWriter; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class CSVFile { public static void main(String[] args) { Scanner fin = null; PrintWriter fout = null; String filename = "csv.txt"; 35

Application Deconstructed try { fin = new Scanner(new File(filename)); fout = new PrintWriter("csvout.txt"); } catch (FileNotFoundException e) { System.err.println(e); System.exit(1); }// end try 36

Application Deconstructed String line = null; int pos; // Read from the input file and write to output file. while (fin.hasNextLine()) { line = fin.nextLine(); // Write the record number. pos = line.indexOf(","); fout.print(line.substring(0, pos) + ":"); line = line.substring(pos + 1); // Write the first name. pos = line.indexOf(","); fout.print(line.subSequence(0, pos) + ":"); line = line.substring(pos + 1); // Write the last name. fout.println(line); }// end while 37

Application Deconstructed fin.close(); fout.close(); System.out.println(filename + " was read and " + "csvout.txt was written."); }// end main() }// end CSVFile 38

Application Deconstructed 39

Recap  Use a File object for useful file information  A csv file requires special handling (for now anyway) 40

10.6 Graphics Supplement 41

Application Deconstructed package fileframe; import javax.swing.JFrame; import javax.swing.JTextArea; import java.awt.Container; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class FileFrame extends JFrame { JTextArea contentsTextArea; public static void main(String[] args) { FileFrame frame = new FileFrame(); frame.setSize(600, 500); frame.setTitle("File Viewer :: fileOutput.txt"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }// end main() 42

Application Deconstructed // Constructor. public FileFrame() { initComponents(); }// end FileFrame() private void initComponents() { // Create the TextArea. contentsTextArea = new JTextArea(); Container contentPane = getContentPane(); // Add the control to the content pane. contentsTextArea.setSize(590, 470); contentPane.add(contentsTextArea); // Load the text file contents into the TextArea. LoadTextAreaWithFile("fileOutput.txt"); }// end initComponents() 43

Application Deconstructed private void LoadTextAreaWithFile(String filename) { Scanner fin = null; try { fin = new Scanner(new File(filename)); } catch (FileNotFoundException e) { System.err.println(e); System.exit(1); }// end try while (fin.hasNextLine()) { contentsTextArea.append(fin.nextLine() + "\n"); }// end while fin.close(); }// end LoadTextAreaWithFile() }// end FileFrame 44

Application Deconstructed 45