File Frequency
//program that displays number of characters,lines,words in a text file.
import java.lang.*;
import java.util.*;
import java.io.*;
class FileCount
{
public static int words=0;
public static int lines=0;
public static int chars=0;
public static void wc(InputStreamReader isr)throws IOException
{
int c=0;
while((c=isr.read())!=-1)
{
chars++;
if(c=='\n')
lines++;
if(c=='\t' || c==' ' || c=='\n')
++words;
}
}
public static void main(String args[])
{
String fname=" ";
System.out.println("enter a file name:");
try
{
DataInputStream dis=new DataInputStream(System.in);
fname=dis.readLine();
FileReader fr=new FileReader(fname);
wc(fr);
System.out.println("the no.of lines in file:"+lines);
System.out.println("the no.of words in file:"+words);
System.out.println("the no.of characters in file:"+chars);
}
catch(Exception e)
{
System.out.println("given file"+fname+"does not exist");
}
}
}
import java.lang.*;
import java.util.*;
import java.io.*;
class FileCount
{
public static int words=0;
public static int lines=0;
public static int chars=0;
public static void wc(InputStreamReader isr)throws IOException
{
int c=0;
while((c=isr.read())!=-1)
{
chars++;
if(c=='\n')
lines++;
if(c=='\t' || c==' ' || c=='\n')
++words;
}
}
public static void main(String args[])
{
String fname=" ";
System.out.println("enter a file name:");
try
{
DataInputStream dis=new DataInputStream(System.in);
fname=dis.readLine();
FileReader fr=new FileReader(fname);
wc(fr);
System.out.println("the no.of lines in file:"+lines);
System.out.println("the no.of words in file:"+words);
System.out.println("the no.of characters in file:"+chars);
}
catch(Exception e)
{
System.out.println("given file"+fname+"does not exist");
}
}
}
Labels: Java Programming Lab
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home