0% found this document useful (0 votes)
10 views

Java Lect 16

This document summarizes a lecture on file handling in Java programming. It discusses revising file reading, writing to files, and provides examples of file handling programs. Specifically, it covers opening and reading files, opening and writing to files, and examples that copy a file, expand abbreviations by reading from and writing to multiple files, and count dictionary entries by reading a file line by line. The document provides source code examples and algorithms for accomplishing these file handling tasks.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java Lect 16

This document summarizes a lecture on file handling in Java programming. It discusses revising file reading, writing to files, and provides examples of file handling programs. Specifically, it covers opening and reading files, opening and writing to files, and examples that copy a file, expand abbreviations by reading from and writing to multiple files, and count dictionary entries by reading a file line by line. The document provides source code examples and algorithms for accomplishing these file handling tasks.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Introduction to Programming

With Java

Lecture - 16
UMESH PATIL ([email protected])
[email protected]

Contents for Todays Lecture

Revision of File-Reading
Writing to files
Example programs on file handling

[email protected]

Revision of File-Reading
Commands for opening the file for reading:
FileReader fr = new FileReader(input.txt);
BufferedReader br = new BufferedReader(fr);
Command for reading one line from the opened file:
String str = br.readLine();
Command for closing the opened file:
br.close();
[email protected]

FileRead.java

[email protected]

File-Writing
Commands for opening the file for writing:
FileWriter fw = new FileWriter(output.txt);
BufferedWriter bw = new BufferedWriter(fr);
Command for writing a String to the opened file:
bw.write(str); // str is a String
Command for closing the opened file:
bw.close();
[email protected]

FileWrite.java

[email protected]

Examples
1. Write a File-Copy program which copies the content of
one file to another. Take both the file names from the user.
2. Write a Java program for expanding abbreviations in the
input text. Store the expanded output in one file. Input
text is read from a file. Get the input and output file
names from the user. Abbreviations along with their
expansions are stored in the file abbreviations.txt in the
format:
<line i>abbreviation
<line i+1>expansion

[email protected]

ExpandAbbreviation Algorithm
1. Read abbreviations & expansions from file and store in arrays
2. Open input file for reading
3. Open output file for writing
4. For each line in the input file
1. Search for each abbreviation in the line and replace if found
(find_and_replace() function)
2. Write the modified line to the output file

5. Close all opened files


[email protected]

ExpandAbbreviation.java

[email protected]

Exercise
Write a Java program for reading a dictionary file for
counting the no. of dictionary entries in the file. Each line in
the file consists of one dictionary entry.

[email protected]

End

Thank you

[email protected]

You might also like