Download presentation
Presentation is loading. Please wait.
Published byJeffery Heath Modified over 9 years ago
1
Java ashishfa
2
? How to read file in Java How to read file in Java Developing application to expand abbreviation in given text Developing application to expand abbreviation in given text –“Mr. Vasant and Mr. Gajanan went to IIT” –“Mister Vasant and Mister Gajanan went to Indian Institute of Technology”
3
Files for us Files can store text, formatted reports, pictures, songs, animations and programs Files can store text, formatted reports, pictures, songs, animations and programs Names of files Names of files –report.doc –Readme.txt –Flower.jpg Text files means files that contain text (no other symbols.) i.e. the files that can be read through Notepad or editplus or vi in unix.
4
Files for computers File has name in computer. File has name in computer. Java program cam read a file or write to a file or can modify the contents of file. Java program cam read a file or write to a file or can modify the contents of file. File is treated as a resource. File is treated as a resource. We need to open and close files while using them We need to open and close files while using them To use files To use files –It is assigned a name –It is then opened by open command –Then read /write commands are used –Then file is closed
5
FileReader fr = new FileReader("sample.txt"); BufferedReader bfr = new BufferedReader(fr); String str = bfr.readLine(); System.out.println("File contains"); while(str != null) { System.out.println(str); str = bfr.readLine(); } bfr.close();
6
Abbreviation expanding Logic Logic –Store abbreviations and corresponding expansions –Search them –Replace them –Repeat
7
data representation Ab.Expansion 0Mr.Mister 1Ms. Miss 2Dr. Doctor 3MA Master of arts
8
Find and replace “Hello! Mr. Vasant” “Hello! Mr. Vasant” 0 71016 Mr.Mr. 02 1.>>Hello! << 2.>>Hello! Mister<< 3.>>Hello! Mister Vasant<<
9
String functions used indexof(String) indexof(String) substring(a,b) substring(a,b) substring(a) substring(a) length() length() ab a end a
10
See the code Abbr.java
11
Abbreviation expansion using file Storing abbreviations in arrays in program is not convenient Storing abbreviations in arrays in program is not convenient Better way is to isolate data and program Better way is to isolate data and program –i.e. keep abbreviations in seperate file –Read the abbreviations from this file Modify abbreviation program Modify abbreviation program
12
Abbreviation file Mr.MisterDr.DoctorMA Master of arts... Odd line number Even line number 6 lines i.e. 3 entries 0 Mr.Mister 1 Dr.Doctor Single entry
13
exercise Write a program to search a text file and print only those lines which has the search word in it. Write a program to search a text file and print only those lines which has the search word in it. –Take i/p word from user –Open the text file –Read line by line from it –Search for i/p word in each line –Close file at the end
14
Thank You ;)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.