Reading/Writing Files, Webpages CS2110, Recitation 8 1.

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

Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lecture 15: I/O and Parsing
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.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism.
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.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
Files and Streams CS 21a. 10/02/05 L18: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
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.
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.
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 ->
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Reading/Writing Files, Webpages CS2110, Recitation 10 1.
Performance measurements for inter-process communication.
Java I/O Input: information brought to program from an external source
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Java Exception Handling Handling errors using Java’s exception handling mechanism.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
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.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
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.
1 Streams Files are a computer’s long term memory Need ability for programs to –get information from files –copy information from program variables to.
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,
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
Programs & Data Files Notes Data information processed by word processing, spreadsheet or other application programs are stored as data files. Java programs,
CS 11 java track: lecture 6 This week: networking basics Sockets Vectors parsing strings.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
File IO Basics By Dan Fleck Coming up: Data Streams.
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.
Characters, Strings, Reading Files CS2110, Week 2 Recitation.
Chapter 15 Text Processing and File Input/Output Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Lecture 5 I/O and Parsing
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.
Read and Write Files  By the end of this lab you will be able to:  Write a file in internal storage  Read a file from internal storage  Write a file.
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.
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.
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.
Programs & Data Files Notes Data information processed by word processing, spreadsheet or other application programs are stored as data files. Java.
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.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
java.io supports console and file I/O
Lecture 8: I/O Streams types of I/O streams Chaining Streams
OO Design and Programming II I/O: Reading and Writing
Lesson 8: More File I/O February 5, 2008
Reading from a file A file is typically stored on your computers hard drive. In the simplest case, lets just assume it is text. For a program to use.
Program Input/Output (I/O)
Strings and File I/O.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
File class File myFile=new File(“c:/javaDemo/aa
הרצאה 12: קבצים וחריגות (Exceptions)
CHAPTER 5 (PART 2) JAVA FILE INPUT/OUTPUT
“Introduction to Programming With Java”
Reading and Writing Text Files
18 File i/o, Parsing.
Text File Read and Write Method
Web Design & Development Lecture 8
File Input and Output.
ECE 122 April 14, 2005.
EEC 484/584 Computer Networks
Java IO Packages Prepared by Mrs.S.Amudha AP/SWE
Presentation transcript:

Reading/Writing Files, Webpages CS2110, Recitation 8 1

Reading files/ webpages I/O classes are in package java.io. To import the classes so you can use them, use import java.io.*; 2

Class File An object of class File contains the path name to a file or directory. Class File has lots of methods, e.g. f.exists() f.canRead() f.canWrite() f.delete() f.createNewFile() f.length() … (lots more) … File f= new File(“res/map1.xml”); 3 File path is relative to the package in which the class resides. Can also use an absolute path. To find out what absolute path’s look like on your computer, use f.getAbsolutePath();

Class File f.isdirectory() f.listFiles() f.list() f.mkdir() Suppose f contains a File that describes a directory. Store in b a File[] that contains a File element for each file or directory in directory given by f File[] b= f.listFiles() 4 f.list(): return an array of file and directory names as Strings, instead of as File objects f.Mkdir(): create the directory if it does not exist.

Input Streams Stream: a sequence of data values that is processed —either read or written— from beginning to end. We are dealing with input streams. Read input stream for a file is by creating an instance of class FileReader: FileReader fr= new FileReader(f); fr.read() // get next char of file Too low-level! Don’t want to do char by char. 5 f can be a File or a String that gives the file name

Reading a line at a time Class BufferedReader, given a FileReader object, provides a method for reading one line at a time. FileReader fr= new FileReader(f); BufferedReader br= new BufferedReader(fr); Then: String s= br.readLine(); // Store next line of file in s // (null if none) When finished with reading a file, it is best to close it! br.close(); 6

/** Return number of lines in f. Throw IO Exception if problems encountered when reading */ public static int getSize(Filef) throws IOException { FileReader fr= new FileReader(f); BufferedReader br= new BufferedReader(fr); int n= 0; // number of lines read so far String line= br.readLine(); while (line != null) { n= n+1; line= br.readLine(); } br.close(); return n; } Example: counting lines in a file Don’t forget!Always use this pattern to read a file! line= first line; while (line != null) { Process line; line= next line; } (write as while loop) 7

line= br.readLine(); while (line != null) { Process line line= br.readLine(); } Pattern to read a file Always use this pattern to read a file! line= first line; while (line != null) { Process line; line= next line; } 8

Class URL in package java.net URL url= new URL(“ … …. /links.html); 9 A URL (Universal Resource Locator) describes a resource on the web, like a web page, a jpg file, a gif file The “protocol” can be: http (HyperText Transfer Protocol) https ftp (File Transfer Protocol)

Reading from an html web page Given is URL url= new URL(“ … …. /links.html); To read lines from that webpage, do this: 10 1.Create an InputStreamReader: InputStreamReader isr= new InputStreamReader(url.openStream()); Have to open the stream 2. Create a Buffered Reader: BufferedReader br= new BufferedReader(isr); 3. Read lines, as before, using br.readLine()

javax.swing.JFileChoooser Want to ask the user to navigate to select a file to read? 11 JFileChooser jd= new JFileChooser(); jd.setDialogTitle("Choose input file"); int returnVal= jd.showOpenDialog(null); returnVal is one of JFileChooser.CANCEL_OPTION JFileChooser.APPROVE_OPTION JFileChooser.ERROR_OPTION File f= jd.getSelectedFile(); jd.showOpenDialog("/Volumes/Work15A/webpage/ccgb/"); Starting always from the user’s directory can be a pain for the user. User can give an argument that is the path where the navigation should start

Writing files Writing a file is similar. First, get a BufferedWriter: FileWriter fw= new FileWriter(“the file name”, false); BufferedWriter bw= new BufferedWriter(fw); Then use bw.write(“…”); to write a String to the file. 12 false: write a new file true: append to an existing file bw.close(); // Don’t forget to close!