Pages

Ads 468x60px

Showing posts with label Files. Show all posts
Showing posts with label Files. Show all posts

Monday, 17 December 2012

Check n.o of lines,words,characters in text file



13.      WAJP 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



12.      WAJP 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");
 
  }
 
}    
 

Sample text

Sample Text

Sample Text