Write a Java program to accomplish the following task.
i. Create a text file and store data in it.
ii. Count number of lines and words in the file.
iii. Copy contents of one file to another file.
import java.io.*;
class PerformTask
{
void task()
{
char c;
int words=1,lines=1,chars=0;
try
{
RandomAccessFile ff = new RandomAccessFile("test.txt","r");
while(ff.getFilePointer()<ff.length())
{
c = (char)ff.read();
if(c ==' ')
words++;
else if(c=='\n')
{
lines++;
words++;
}
else
chars++;
}
System.out.println("Words = "+words+" lines = "+lines);
}
catch(IOException ie){}
}
public static void main(String args[])
{
PerformTask p = new PerformTask();
p.task();
}
}
C:\>javac PerformTask.java
C:\>java PerformTask
Words = 7 lines = 3