Showing posts with label Java program to check how many files in a folder. Show all posts
Showing posts with label Java program to check how many files in a folder. Show all posts

Friday, 2 January 2015

Chose file through path in Java

Structure of the Problem Requirements 

In this problem we will learn how to chose a file by giving its path in Java program. The program will prompt user to enter the file path. When user enter the path the program will check rather it is file or folder. If the given path is a file's path then program will chose that file and if it is the folder's path the program will display its length and all files containing that. Here is the source code of this program which help you in better understanding.

Source Code


import java.util.Scanner;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input=new Scanner(System.in);
System.out.println("Enter Path: ");
analyzeFile(input.nextLine());
}
public static void analyzeFile(String path) {
File myFile=new File(path);
if (myFile.exists()) {
System.out.println(myFile.isFile() ? "This is file" : "This is not file");
System.out.println(myFile.isDirectory() ? "This is directory" : "This is not directory");
System.out.println("Length: "+myFile.length());
if (myFile.isDirectory()) {
String[] myDirectory=myFile.list();
System.out.println("This Folder Contains:");
for (String i : myDirectory) {
System.out.println(i);
}
}
}
}
}

Output of the Program


Chose file through path in Java
Chose file through path in Java