CS8382-OBJECT ORIENTED PROGRAMMING LABORATORY-FILE HANDLING METHODS

CS8382-OBJECT ORIENTED PROGRAMMING LABORATORY


QUESTION:Write a Java program that reads a file name from the user, displays information about whether the file exists, whether the file is readable, or writable, the type of file and the length of the file in bytes

PROGRAM:

import java.io.*;
import java.util.Scanner;
class Opfile{
    Scanner sc= new Scanner(System.in);
void show(){
    String fname;
//getting file name from user
System.out.println("enter file name");
        fname=sc.next();
        //assign as file name
        File f= new File(fname); 
        String line=null;
        try
        { 
            FileReader fileReader=new FileReader(f);                       
            BufferedReader bufferedReader=new BufferedReader(fileReader);           
            while((line=bufferedReader.readLine()) != null)
            {
                System.out.println(line);
            }  
            bufferedReader.close();
        }
        catch(IOException ex)
        {
            System.out.println("Error reading file named '" + f + "'");
        }
 
System.out.println("file name is :"+f);
}
}
class Existsfile extends Opfile{
void ex(){
String fname;
//getting file name from user
System.out.println("enter file name");
        fname=sc.next();
        //assign as file name
        File f= new File(fname); 
        System.out.println("Exists :"+f.exists()); 
        System.out.println("Absolute path:" +f.getAbsolutePath());
}
}

class Rw extends Opfile{
void findrw(){
String fname;
//getting file name from user
System.out.println("enter file name");
        fname=sc.next();
        //assign as file name
        File f= new File(fname); 
System.out.println("Exists :"+f.exists()); 
        if(f.exists()) 
        { 
            System.out.println("Is writeable:"+f.canWrite()); 
            System.out.println("Is readable"+f.canRead());            
            System.out.println("File Size in bytes "+f.length()); 
        } 
}
}
class Extensionf extends Opfile{

void getFileExtension(){
        String extension="";
String fname;
//getting file name from user
System.out.println("enter file name");
        fname=sc.next();
        //assign as file name
        File f= new File(fname); 
 
        try {
            if (f!=null&&f.exists()) {
                String name=f.getName();
                extension=name.substring(name.lastIndexOf("."));
            }
        } catch(Exception e) {
            extension = "";
        }
 
        System.out.println(extension);
 
    }
}
public class Fileoperations 
{   
    public static void main(String[] args) { 
         int op;
while(true){
System.out.println("enter \n 1.display file \n2. check file exists or not\n3.check file readable & writeable get file length in bytes\n4.find file type\n");
         Scanner in=new Scanner(System.in);
     op=in.nextInt();
switch(op){    
    case 1:
      Opfile obj=new Opfile();
  obj.show();
          break;    
    case 2:
      Existsfile obj1=new Existsfile();
          obj1.ex();
    break;    
    case 3: 
      Rw obj2= new Rw();
          obj2.findrw();
          break;    
    case 4:
     Extensionf obj3=new Extensionf();
obj3.getFileExtension();
          break; 
        
    default:System.out.println("Invalid input");    
    }    
        
    } 
}
}
--------------------------------------------------------------------

OUTPUT

run:
enter 
 1.display file 
2. check file exists or not
3.check file readable & writeable get file length in bytes
4.find file type

1
enter file name
open.txt
line one
 line two
line three
line four
line five
line six
end of the text file
thanking you!!!
file name is :open.txt
enter 
 1.display file 
2. check file exists or not
3.check file readable & writeable get file length in bytes
4.find file type

2
enter file name
open.txt
Exists :true
Absolute path:C:\Users\prawin\Documents\NetBeansProjects\Fileoperations\open.txt
enter 
 1.display file 
2. check file exists or not
3.check file readable & writeable get file length in bytes
4.find file type

3
enter file name
open.txt
Exists :true
Is writeable:true
Is readabletrue
File Size in bytes 102
enter 
 1.display file 
2. check file exists or not
3.check file readable & writeable get file length in bytes
4.find file type

4
enter file name
open.txt
.txt
enter 
 1.display file 
2. check file exists or not
3.check file readable & writeable get file length in bytes
4.find file type


-------------------------------------------------------------------------------------

Note:
Before executeing this program you have to create text file & place on same folder
example.....C:\Users\prawin\Documents\NetBeansProjects\Fileoperations\open.txt
my text file is:
-----------------------------------------------------------------------------------------
line one
line two
line three
line four
line five
line six
end of the text file
thanking you!!!
-------------------------------------------------------------------------------------

if any one having doubt comment here i will explain clearly .... Thanks for your support ....

Comments

Popular posts from this blog

CS8461- OPERATING SYSTEMS LABORATORY-Write C programs to simulate UNIX commands like cp, ls, grep, etc.

CS8382-OBJECT ORIENTED PROGRAMMING LABORATORY-JAVA EMPLOYEE PROGRAM USING INHERITANCE CONCEPT FOR

CS8382-OBJECT ORIENTED PROGRAMMING LABORATORY-TO GERNERATE EMPLOYEE SALARY SLIP PROGRAM IN JAVA