Files and Streams. Goals To be able to read and write text files To be able to read and write text files To become familiar with the concepts of text.

Slides:



Advertisements
Similar presentations
Reading and Writing Text Files Svetlin Nakov Telerik Corporation
Advertisements

Lecture 15: I/O and Parsing
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.
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
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 
Lecture 7 File I/O (and a little bit about exceptions)‏
File I/O in Java CS 311, Winter File Basics Recall that a file is block structured. What does this mean? What happens when an application opens.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
Chapter 16 – Files and Streams. Goals To be able to read and write text files To be able to read and write text files To become familiar with the concepts.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
Chapter 16 – Files and Streams. Announcements Only responsible for 16.1,16.3 Only responsible for 16.1,16.3 Other sections “encouraged” Other sections.
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.
Input/Ouput and Exception Handling. 2 Exceptions  An exception is an object that describes an unusual or erroneous situation  Exceptions are thrown.
Two Ways to Store Data in a File Text format Binary format.
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.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
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.
Program data (instance variables, local variables, and parameters) is transient, because its lifetime ends with the program...if not, before. Sometimes.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
I/O in Java Dennis Burford
File Input/Output. 2Java Programming: From Problem Analysis to Program Design, 3e File Input/Output File: area in secondary storage used to hold information.
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.
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.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Chapter 10 Text Files Section 10.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Week 12 – Text Files Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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.
File Input and Output Appendix E © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
COM S 207 File Instructor: Ying Cai Department of Computer Science Iowa State University
Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
GENERICS AND FILE HANDLING Saumya Srivastava (977934) Divyangana Pandey (977790) Shubhi Saxena (978108) Arka Das (962969) AHD05/15-16 AJA 21.
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
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.
CSC 211 Java I File I/O and more methods. Today’s plan Homework discussion Reading lines from files Writing lines to files All of the above, using methods.
IO in java.
OO Design and Programming II I/O: Reading and Writing
Basic Text File Input/Output
File - CIS 1068 Program Design and Abstraction
Text File Input/Output
File Input / Output.
Streams & File Input/Output (I/O)
CMSC 202 Text File I/O.
Lesson 8: More File I/O February 5, 2008
Java Programming Lecture 2
Streams and File I/O.
Text File Input/Output
I/O Basics.
Working with files.
Streams and File I/O Chapter 14.
CSS 161: Fundamentals of Computing
18 File i/o, Parsing.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
File Input and Output.
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:

Files and Streams

Goals To be able to read and write text files To be able to read and write text files To become familiar with the concepts of text and binary formats To become familiar with the concepts of text and binary formats

Reading and Writing Text Files Text files – files containing simple text Text files – files containing simple text Created with editors such as notepad, html, etc. Created with editors such as notepad, html, etc. Simplest way to learn it so extend our use of Scanner Simplest way to learn it so extend our use of Scanner Associate with files instead of System.in Associate with files instead of System.in All input classes, except Scanner, are in java.io All input classes, except Scanner, are in java.io import java.io.*; import java.io.*;

Review: Scanner We've seen Scanner before We've seen Scanner before The constructor takes an object of type java.io.InputStream – stores information about the connection between an input device and the computer or program The constructor takes an object of type java.io.InputStream – stores information about the connection between an input device and the computer or program Example: System.in Example: System.in Recall – only associate one instance of Scanner with System.in in your program Recall – only associate one instance of Scanner with System.in in your program Otherwise, get bugs Otherwise, get bugs

Numerical Input 2 ways (we’ve learned one, seen the other) 2 ways (we’ve learned one, seen the other) Use int as example, similar for double Use int as example, similar for double First way: First way: Use nextInt() Use nextInt() int number = scanner.nextInt(); Second way: Second way: Use nextLine(), Integer.parseInt() Use nextLine(), Integer.parseInt() String input = scanner.nextLine(); int number = Integer.parseInt(input);

Numerical Input Exceptions Exceptions nextInt() throws InputMismatchException nextInt() throws InputMismatchException parseInt() throws NumberFormatException parseInt() throws NumberFormatException Optimal use Optimal use nextInt() when there is multiple information on one line nextInt() when there is multiple information on one line nextLine() + parseInt() when one number per line nextLine() + parseInt() when one number per line

Reading Files The same applies for both console input and file input The same applies for both console input and file input We can use a different version of a Scanner that takes a File instead of System.in We can use a different version of a Scanner that takes a File instead of System.in Everything works the same! Everything works the same!

Reading Files To read from a disk file, construct a FileReader To read from a disk file, construct a FileReader Then, use the FileReader to construct a Scanner object Then, use the FileReader to construct a Scanner object FileReader rdr = newFileReader("input.txt"); Scanner fin = new Scanner(rdr);

Reading Files You can use File instead of FileReader You can use File instead of FileReader Has an exists() method we can call to avoid FileNotFoundException Has an exists() method we can call to avoid FileNotFoundException File file = new File ("input.txt"); Scanner fin; if(file.exists()){ fin = new Scanner(file); } else { //ask for another file }

Reading Files Once we have a Scanner, we can use methods we already know: Once we have a Scanner, we can use methods we already know: next, nextLine, nextInt, etc. next, nextLine, nextInt, etc. Reads the information from the file instead of console Reads the information from the file instead of console

File Class java.io.File java.io.File associated with an actual file on hard drive associated with an actual file on hard drive used to check file's status used to check file's status Constructors Constructors File( ) File( ) File(, ) File(, ) Methods Methods exists() exists() canRead(), canWrite() canRead(), canWrite() isFile(), isDirectory() isFile(), isDirectory()

File Class java.io.FileReader java.io.FileReader Associated with File object Associated with File object Translates data bytes from File object into a stream of characters (much like InputStream vs. InputStreamReader) Translates data bytes from File object into a stream of characters (much like InputStream vs. InputStreamReader) Constructors Constructors FileReader( ); FileReader( ); Methods Methods read(), readLine() read(), readLine() close() close()

Writing to a File We will use a PrintWriter object to write to a file We will use a PrintWriter object to write to a file What if file already exists?  Empty file What if file already exists?  Empty file Doesn’t exist?  Create empty file with that name Doesn’t exist?  Create empty file with that name How do we use a PrintWriter object? How do we use a PrintWriter object? Have we already seen one? Have we already seen one?

Writing to a File The out field of the System class is a PrintWriter object associated with the console The out field of the System class is a PrintWriter object associated with the console We will associate our PrintWriter with a file now We will associate our PrintWriter with a file now PrintWriter fout = new PrintWriter("output.txt"); fout.println(29.95); fout.println(new Rectangle(5, 10, 15, 25)); fout.println("Hello, World!"); This will print the exact same information as with System.out (except to a file “output.txt”)! This will print the exact same information as with System.out (except to a file “output.txt”)!

Closing a File Only main difference is that we have to close the file stream when we are done writing Only main difference is that we have to close the file stream when we are done writing If we do not, not all output will written If we do not, not all output will written At the end of output, call close() At the end of output, call close()fout.close();

Closing a File Why? Why? When you call print() and/or println(), the output is actually written to a buffer. When you close or flush the output, the buffer is written to the file When you call print() and/or println(), the output is actually written to a buffer. When you close or flush the output, the buffer is written to the file The slowest part of the computer is hard drive operations – much more efficient to write once instead of writing repeated times The slowest part of the computer is hard drive operations – much more efficient to write once instead of writing repeated times