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


Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

0 comments:

Post a Comment