Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
38 views

Fileinfo - Java: S.No: 18 Date: 2022-06-15

The document describes a Java program that reads a file name from the user and displays information about the file. The program checks if the file exists, if it is readable and writable, determines the file type, and displays the length of the file in bytes. The source code provided implements this program by getting the file name from the user, creating a File object, and printing details about the file such as whether it exists, if it can be read/written, and its length. The program is tested by passing different file names and verifying the output.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Fileinfo - Java: S.No: 18 Date: 2022-06-15

The document describes a Java program that reads a file name from the user and displays information about the file. The program checks if the file exists, if it is readable and writable, determines the file type, and displays the length of the file in bytes. The source code provided implements this program by getting the file name from the user, creating a File object, and printing details about the file such as whether it exists, if it can be read/written, and its length. The program is tested by passing different file names and verifying the output.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exp.

Name: Write a Java program that reads a file name from the user then displays
S.No: 18 Date: 2022-06-15

ID: 20095A0363
    Page No:
           
information

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

Source Code:

FileInfo.java

import java.io.*;

class FileInfo

public static void main(String args[])throws Exception

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter file name(text1.txt or text2.txt): ");

String fname=br.readLine();

File f=new File(fname);

System.out.println("File existance: " +f.exists());

System.out.println("File writable: " +f.canWrite());

System.out.println("File readable: " +f.canRead());

System.out.println("File length(in bytes): " +f.length() +" Bytes");

    Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous)      2019-2023-MECH-FDH


}

text1.txt

We know that we cannot save our environment overnight. But, having an intention
to make

possible is all that count. Reduce wastage of papers. Try not to ruin plants. Th
ey

are one of the biggest factors that can keep the earth fertile and save the lif
e. Save

trees to save our environment. Encourage your child to plant trees in any empty
ground.

As I said before, the initiative counts. If you feel the need of saving the envi
ronment,

you will stop others from doing something which can hurt the well being of the n
ature.

text2.txt

Save Our Environment by Being Responsible:

Your every action will count. You should not only hold others responsible, make
yourself responsible too.

Why not start saving our environment being a little less self-concerned. Sometim
es give priority to the

nature before giving priority to yourself. Save the energy, save plants and be s
ympathetic to the nature

surrounding us.

ID: 20095A0363
    Page No:
           
Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter file name(text1.txt or text2.txt): text.txt
File existance: false
File writable: false
File readable: false
File length(in bytes): 0 Bytes

Test Case - 2

User Output
Enter file name(text1.txt or text2.txt): text1.txt
File existance: true
File writable: true
File readable: true
File length(in bytes): 521 Bytes

    Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous)      2019-2023-MECH-FDH

You might also like