Monday 4 May 2015

Java Program to Check number of lines, words,characters in text file


Java Program that displays the number of characters, lines and words in a text/text file.
Program:
                                    Download link to Filestatus
import java.io.*;

public class FileStat
{
  public static void main(String args[])throws IOException
  {
    long n1=0,nw=0,nc=0;
   
    String line;
   
    BufferedReader br=new BufferedReader(new FileReader(args[0]));
   
    while((line=br.readLine())!=null)
    {
      nl++;
     
      nc=nc+line.length();
     
      int i=0;
     
      boolean pspace=true;
     
      while(i<line.length())
      {
       char c=line.charAt(i++);
      
       boolean cspace=Character.isWhitespace(c);
      
       if(pspace&&!cspace)
        
         nw++;
        
       pspace=cspace;
      }
   }       



FileInputstream class
     Java Program that reads on file name from the user then displays information about whether the file exists, whether the file is readable/writable, the type of file and the length of the file in bytes and display the content of the using FileInputStream class.

Program:
                                           Download link to Fileinputstream

import java.io.File;
import java.lang.String;
import java.util.*;

class FileDemo
{
  static void file(String s)
  {
    System.out.println(s);
  }
 public static void main(String args[])
 {
   File f1=new File(args[0]);
  
   System.out.println("File Name:"+f1.getName());
  
   System.out.println("Path:"+f1.getPath());
  
   System.out.println("Abs Path:"+f1.getAbsolutePath());
  
   System.out.println("Parent:"+f1.getParent());
  
   System.out.println(f1.exists()?"exists":"does not exist");
  
   System.out.println(f1.canWrite()?"is writable":"is not writable");
  
   System.out.println(f1.canRead()?"is readable":"is not readable");
  
   System.out.println("is"+(f1.isDirectory()?"":"not"+"a directory"));
  
   System.out.println(f1.isFile()?"is normal file":"might be a named pipe");
  
   System.out.println(f1.isAbsolute()?"is absolute":"is not absolute");
  
   System.out.println("File last modified:"+f1.lastModified());
  
   System.out.println("file size:"+f1.length()+"Bytes");
 
  }
 
}    

7 comments: