0% found this document useful (0 votes)
13 views2 pages

Java File Handling Demo

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Java File Handling Demo

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Programming

[Link]

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

Rajeswari college of arts and science for women


Java Programming

Output:
Enter the name of the file:
D:\JAVA\Programs\Sample\src\[Link]
The fileD:\JAVA\Programs\Sample\src\[Link] not exists

Rajeswari college of arts and science for women

Common questions

Powered by AI

To extend the FileDemo program for copying or moving files, Java's Files utility class could be used. This requires addressing considerations such as ensuring that destination paths exist, handling potential IOExceptions, and confirming that the source file is indeed readable . The program must also check write permissions for the destination. It should ensure that operations do not inadvertently override files at the destination unless explicitly allowed by the user .

The FileDemo program prompts the user to input the file name, then it creates a File object with that name . It checks the file's existence using the exists() method. If the file exists, it further examines its readability with canRead() and writability with canWrite(). Additionally, it determines the file type by extracting the substring after the last dot in the file name, and measures the file length using the length() method .

The FileDemo program assumes that the file type can be deduced from the file name's extension. It identifies the file type by taking the substring of the file name after the last dot '.' character . This relies on the convention that file extensions denote file types, an assumption rooted in common file naming practices but which may not always hold true if files are improperly named or if they lack extensions .

Potential errors in the FileDemo program include incorrect file extensions if the file name is malformed, exceptions if the file path is invalid, or unexpected behavior on platforms that do not use extensions to determine file types . These can be mitigated by adding exception handling for invalid files and paths, and implementing logic to handle edge cases where files do not have extensions . Further, enhancing the program to check for multiple dots and handle hidden files could also improve robustness .

You might also like