0% found this document useful (0 votes)
60 views16 pages

Topic 10 - Files and Input Output

The document discusses input/output in Java, including reading and writing files and data from the web. It covers using the File class to obtain file properties, reading and writing text files using Scanner and PrintWriter, and automatically closing resources with try-with-resources. It also provides an example of replacing text in a file and describes a case study of developing a basic web crawler that follows hyperlinks by maintaining lists of pending and traversed URLs.

Uploaded by

Amiel Harith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views16 pages

Topic 10 - Files and Input Output

The document discusses input/output in Java, including reading and writing files and data from the web. It covers using the File class to obtain file properties, reading and writing text files using Scanner and PrintWriter, and automatically closing resources with try-with-resources. It also provides an example of replacing text in a file and describes a case study of developing a basic web crawler that follows hyperlinks by maintaining lists of pending and traversed URLs.

Uploaded by

Amiel Harith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Topic 10:

Files Input/Output

Liang, Introduction to Java Programming, Eleventh Edition (2018), Pearson Education Ltd
2 Objectives

 To read data from a file using the Scanner class .


 To understand how data is read using a Scanner.
 To develop a program that replaces text in a file.
 To read data from the Web.
 To develop a Web crawler.
3
The File Class

 The File class is intended to provide an abstraction that deals with


most of the machine-dependent complexities of files and path names in
a machine-independent fashion.
 The filename is a string.
 The File class is a wrapper class for the file name and its directory
path.
Obtaining file properties and manipulating file

4
5 Problem: Explore File Properties
• Objective: Write a program that demonstrates how to create files
in a platform-independent way and use the methods in the File
class to obtain their properties.
• The following figures show a sample run of the program on
Windows and on Unix.

TestFileClass
6 Text I/O
 A File object encapsulates the properties of a file or a path, but
does not contain the methods for reading/writing data from/to a
file.
 In order to perform I/O, you need to create objects using
appropriate Java I/O classes.
 The objects contain the methods for reading/writing data
from/to a file. This section introduces how to read/write strings
and numeric values from/to a text file using the Scanner and
PrintWriter classes.
7 Writing Data Using PrintWriter

WriteData
8 Try-with-resources

 Programmers often forget to close the file. JDK 7


provides the followings new try-with-resources syntax
that automatically closes the files.

try (declare and create resources) {


Use the resource to process the file;
}

WriteDataWithAutoClose
9 Reading Data Using Scanner

ReadData
10 Problem: Replacing Text

 Write a class named ReplaceText that replaces a string in a text file with
a new string.
 The filename and strings are passed as command-line arguments as
follows:
 java ReplaceText sourceFile targetFile oldString newString
 For example, invoking
 java ReplaceText FormatString.java t.txt StringBuilder StringBuffer
 replaces all the occurrences of StringBuilder by StringBuffer in
FormatString.java and saves the new file in t.txt.

ReplaceText
11 Reading Data from the Web
 Just like you can read data from a file on your computer,
you can read data from a file on the Web.
12 Reading Data from the Web

 URL url = new URL("www.google.com/index.html");

 After a URL object is created, you can use the


openStream() method defined in the URL class to
open an input stream and use this stream to create a
Scanner object as follows:

 Scanner input = new Scanner(url.openStream());

ReadFileFromURL
13 Case Study: Web Crawler
 This case study develops a program that travels the Web by following hyperlinks.
14 Case Study: Web Crawler

 The program follows the URLs to traverse the Web.


 To avoid that each URL is traversed only once, the program
maintains two lists of URLs.
 One list stores the URLs pending for traversing and the other
stores the URLs that have already been traversed.
 The algorithm for this program can be described as follows:
15 Case Study: Web Crawler

Add the starting URL to a list named listOfPendingURLs;


while listOfPendingURLs is not empty {
Remove a URL from listOfPendingURLs;
if this URL is not in listOfTraversedURLs {
Add it to listOfTraversedURLs;
Display this URL;
Exit the while loop when the size of S is equal to 100.
Read the page from this URL and for each URL contained in the page {
Add it to listOfPendingURLs if it is not is listOfTraversedURLs;
}
}
}

WebCrawler
16
END of slides

You might also like