1 / 14

Java

Java . ashishfa. ? . How to read file in Java 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”. Files for us.

nate
Download Presentation

Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java ashishfa

  2. ? • How to read file in Java • 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 • 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. • Java program cam read a file or write to a file or can modify the contents of file. • File is treated as a resource. • We need to open and close files while using them • 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 • File is treated as a resource.

  5. code! 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 • Store abbreviations and corresponding expansions • Search them • Replace them • Repeat

  7. data representation Ab. Expansion 0 Mr. Mister 1 Ms. Miss 2 Dr. Doctor 3 MA Master of arts

  8. Find and replace • “Hello! Mr. Vasant” Mr. 0 0 2 7 10 16 1.>>Hello! << 2.>>Hello! Mister<< 3.>>Hello! Mister Vasant<<

  9. String functions used a • indexof(String) • substring(a,b) • substring(a) • length() a b a end

  10. See the code Abbr.java

  11. Abbreviation expansion using file • Storing abbreviations in arrays in program is not convenient • Better way is to isolate data and program • i.e. keep abbreviations in seperate file • Read the abbreviations from this file • Modify abbreviation program

  12. Abbreviation file Mr. Mister Dr. Doctor MA Master of arts . . . Odd line number 6 lines i.e. 3 entries Even line number 0 Mr. Mister 1 Dr. Doctor

  13. exercise • 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 ;)

More Related