Saturday, 21 July 2012

Java program which takes path of directory as input and displays all files and its names with out extension present in the directory


Java program which takes path of directory as input and displays all files and its names with out extension present in the directory


Put Commons-io 2.3 version jar in lib folder and add to class path and then run


package com.demo;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;

public class InputFolder {
public static void main(String[] args) throws IOException {
InputStreamReader inp = new InputStreamReader(System.in);
   BufferedReader br = new BufferedReader(inp);

   System.out.println("Enter folder name : ");
 

String str = br.readLine();
File f1 = new File(str);
System.out.println("folder name is "+str);
System.out.println("file is "+f1);

fetch_files(f1);

}

private static void fetch_files(File fpath) throws IOException {



List<File> files = (List<File>) FileUtils.listFiles(fpath, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for (File file : files) {
System.out.println("file: " + "  path is  " +file.getCanonicalPath()+"   name is  "+file.getName().substring(0, file.getName().lastIndexOf('.')));
//System.out.println("ar is "+list.getName().substring(0, list.getName().lastIndexOf('.')));

}


}
}

No comments:

Post a Comment

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...