Saturday, 21 May 2022

Write a Java program to display given extension files from a specific directory on server machine.

 DOWNLOAD     SLIP14Q2


/**
* STEPS TO RUN CODE
* Step 01 compile the code
* Step 02 run the code
* Step 03 give a file directory location to find file
* Step 04 give a file extension name without (.)
* and then u will get all file names
*/



import java.io.*;

/**
* Created by : Amar Ghugare
* created on : 21-05-2022 20:44
* Purpose of class : Write a Java program to display
* given extension files from a specific directory on
* server machine.
*/
public class Slip14Q2 {
public static void main(String[] args) throws IOException {
String userFileExtension = "";

BufferedReader bufferedInputStream = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("GIVE A FILE LOCATION TO FIND SPECIFIC TYPE OF FILE");
String userFileLocation = bufferedInputStream.readLine();
File fileLocation = new File(userFileLocation);
if (fileLocation.exists()) {
System.out.println("\nGIVE A FILE EXTENSION TO FIND IN GIVEN DIRECTORY");
userFileExtension = bufferedInputStream.readLine();
String finalUserFileExtension = userFileExtension;

String[] filenamesInDIR = fileLocation.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.toLowerCase().endsWith("." + finalUserFileExtension)) {
return true;
} else {
return false;
}
}
});
assert filenamesInDIR != null;
if(filenamesInDIR.length!=0){
for (String fileName:filenamesInDIR){
System.out.println(fileName);
}
}else {
System.out.println("THERE IS NO FILE OF THAT EXTENSION");
}
}else {
System.out.println("THIS FILE NOT EXIST IF YOU WANT TO RECHECK THEN TYPE 'Y' OR 'N'.");
String recheckOption = bufferedInputStream.readLine();
if("Y".equals(recheckOption.toUpperCase())){
main(args);
}
}
}
}

No comments:

Post a Comment

Write a Java program to display given extension files from a specific directory on server machine.

 DOWNLOAD     SLIP14Q2 /** * STEPS TO RUN CODE * Step 01 compile the code * Step 02 run the code * Step 03 give a file directory locatio...