- PowerPoint PPT Presentation

About This Presentation
Title:

Description:

... FileReader fr = new FileReader( input.txt ); BufferedReader br = new BufferedReader(fr); Command for reading one line from the opened file: ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 12
Provided by: Ume3
Category:

less

Transcript and Presenter's Notes

Title:


1
Introduction to Programming With Java
Lecture - 16 UMESH PATIL (umesh_at_cse.iitb.ac.in)
nlp-ai_at_cse.iitb
2
Contents for Todays Lecture
  • Revision of File-Reading
  • Writing to files
  • Example programs on file handling

nlp-ai_at_cse.iitb
3
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()

nlp-ai_at_cse.iitb
4

FileRead.java
nlp-ai_at_cse.iitb
5
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()

nlp-ai_at_cse.iitb
6


FileWrite.java
nlp-ai_at_cse.iitb
7
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
    formatltline igtabbreviationltline i1gtexpansion

nlp-ai_at_cse.iitb
8
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
  5. Search for each abbreviation in the line and
    replace if found(find_and_replace() function)
  6. Write the modified line to the output file
  7. Close all opened files

nlp-ai_at_cse.iitb
9


ExpandAbbreviation.java
nlp-ai_at_cse.iitb
10
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.

nlp-ai_at_cse.iitb
11
End
Thank you ?
nlp-ai_at_cse.iitb
Write a Comment
User Comments (0)
About PowerShow.com