0% found this document useful (0 votes)
2 views

java lab

The document contains a Java program that checks the existence, readability, and writability of a specified file. It prompts the user to enter a file name and provides information about the file's type and length if it exists. An example output is included, indicating that the specified file does not exist.

Uploaded by

drsaranyarcw
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

java lab

The document contains a Java program that checks the existence, readability, and writability of a specified file. It prompts the user to enter a file name and provides information about the file's type and length if it exists. An example output is included, indicating that the specified file does not exist.

Uploaded by

drsaranyarcw
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Programming

11.FileDemo

Program:
import java.io.*;
import java.util.*;
public class FileDemo {
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("Enter the name of the file:");
String file_name=input.nextLine();
File f=new File(file_name);
if(f.exists())
System.out.println("The file"+file_name+"exists");
else
System.out.println("The file"+file_name+"does not exists");
if(f.exists())
{
if(f.canRead())
System.out.println("The file"+file_name+"is readable");
else
System.out.println("The file"+file_name+"is not readable");
if(f.canWrite())
System.out.println("The file"+file_name+"is writeable");
else
System.out.println("The file"+file_name+"is not writeable");
System.out.println("The file type
is:"+file_name.substring(file_name.indexOf('.')+1));
System.out.println("The Length of the file:"+f.length());
}
}
}

Rajeswari college of arts and science for women


Java Programming

Output:
Enter the name of the file:
D:\JAVA\Programs\Sample\src\DemoFile.txt
The fileD:\JAVA\Programs\Sample\src\DemoFile.txtdoes not exists

Rajeswari college of arts and science for women

You might also like