Answer to Question #291644 in Java | JSP | JSF for rohan khan

Question #291644

To 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.


1
Expert's answer
2022-01-28T10:33:13-0500
import java.io.File;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("File name:");
        String fileName = in.nextLine();
        File file = new File(fileName);
        if (file.exists()) {
            System.out.println("Exist: true");
            System.out.println("Readable: " + file.canRead());
            System.out.println("Writable: " + file.canWrite());
            boolean typeFound = false;
            for (int i = fileName.length() - 1; i >= 0; i--) {
                if (fileName.charAt(i) == '.') {
                    System.out.println("Type: " + fileName.substring(i + 1));
                    typeFound = true;
                    break;
                }
            }
            if (!typeFound) {
                System.out.println("Type: UNKNOWN");
            }

            System.out.println("Length: " + file.length());
        } else {
            System.out.println("Exist: false");
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS