Table of Contents

Java Managing I/O Files Exercise

Problem Statement-1:

Write a Java program to Create a file filename.txt and perform  getName(), getAbsolutePath(), canWrite(), canRead() methods to get information about the file.

Expected Output:

File name: filename.txt
Absolute path: the path of your file
Writeable: true
Readable: true  


import;  // Import the File class
public class GetFileInfo {
 public static void main(String[] args) {
   File f = new File("filename.txt");
   if (f.exists()) {
     System.out.println("File name: " + f.getName());
     System.out.println("Absolute path: " + f.getAbsolutePath());
     System.out.println("Writeable: " + f.canWrite());
     System.out.println("Readable " + f.canRead());
   } else {
     System.out.println("The file does not exist.");
File name: filename.txt
Absolute path: E:\\\\filename.txt
Writeable: true
Readable: true   */


In the class GetFileInfo create a file named filename.txt using class File. The condition

if (f.exists())  states that if the file exits then it enters the if block when the condition is false it prints the file does not exist. The getName(), getAbsolutePath(), canWrite(), canRead() methods are used to get information about the file in java.

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?