Page 347 - computer science (868) class 11
P. 347
Iberis
Continue y/nn
Aster
Iberis
Program 6 Define a class called Poems to perform read and write operations on a text file “Poem.txt”.
The detail of the class is given as follows:
Data Members
String pline : To store lines of a poem
Member Methods
void createFile() : Accepts five lines of a poem and stores them in a text file
“Poem.txt”
void countWrd() : Reads text file “Poem.txt” and counts how many times the word
“divine” is present in the lines
static void main() : Creates object and executes other methods
1 import java.io.*;
2 import java.util.*;
3 class Poems
4 {
5 String pline; // data member
6 void createFile() throws IOException
7 {
8 // declaring the classes required to create file
9 // parameter true opens file in append mode
10 FileWriter fw=new FileWriter("Poem.txt",true);
11 BufferedWriter bw= new BufferedWriter(fw);
12 PrintWriter pw = new PrintWriter(bw);
13 Scanner sc=new Scanner(System.in);
14 for(int i=1;i<=5;i++) // loop to store multiple records
15 {
16 System.out.println("Enter lines of a poem");
17 // entering data using Scanner class
18 pline=sc.nextLine();
19 // writing data to buffer using specific write methods of data type
20 pw.println(pline);
21 sc=new Scanner(System.in);
22 }
23 // closing the objects transfers data in buffer to file permanently
345
Basic Input/Output 345

