//Java code to list all files in a directory and sub-directories
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import com.csvreader.CsvWriter;
public class FilesFromDirectories {
static long fileSize;
static String size;
static CsvWriter csvOutput;
public static <lang> void main(String a[]) throws IOException{
//Folder path
File file = new File(" D://learn");
//Out put csv file
File file2 = new File("D:\\output.csv");
if ( !file2.exists() ){
file2.createNewFile();
}
csvOutput= new CsvWriter(new FileWriter(file2, true), ',');
csvOutput.write("File Name");
csvOutput.write("Creation Time");
csvOutput.write("Size");
csvOutput.write("Full Path");
csvOutput.endRecord();
listfiles(file.toString());
}
public static void listfiles(String directoryName) throws IOException {
File directory = new File(directoryName);
BasicFileAttributes attributes = null;
File[] fList = directory.listfilesiles();
try
{
for (File f : fList) {
if (f.isFile()) {
attributes = Files.readAttributes(f.toPath(), BasicFileAttributes.class);
//System.out.println("File Name ="+f.getName());
fileSize= f.length();
size=String.valueOf(fileSize);
//System.out.println("File Size :: " + fileSize);
// System.out.println("File Size :: " + f);
System.out.println("File Name :: "+ f.getName()+"Size :: "+ size + "Full Path "+f.toString());
System.out.println("Creation Time ::"+attributes.creationTime());
csvOutput.write(f.getName());
csvOutput.write(attributes.creationTime().toString());
csvOutput.write(size);
csvOutput.write(f.toString());
csvOutput.endRecord();
} else if (f.isDirectory()) {
System.out.println(f);
listfiles(f.getAbsolutePath());
}
csvOutput.flush();
}
}
catch(IOException exception)
{
System.out.println("Exception handled when trying to get file " +
"attributes: " + exception.getMessage());
}
}
}
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import com.csvreader.CsvWriter;
public class FilesFromDirectories {
static long fileSize;
static String size;
static CsvWriter csvOutput;
public static <lang> void main(String a[]) throws IOException{
//Folder path
File file = new File(" D://learn");
//Out put csv file
File file2 = new File("D:\\output.csv");
if ( !file2.exists() ){
file2.createNewFile();
}
csvOutput= new CsvWriter(new FileWriter(file2, true), ',');
csvOutput.write("File Name");
csvOutput.write("Creation Time");
csvOutput.write("Size");
csvOutput.write("Full Path");
csvOutput.endRecord();
listfiles(file.toString());
}
public static void listfiles(String directoryName) throws IOException {
File directory = new File(directoryName);
BasicFileAttributes attributes = null;
File[] fList = directory.listfilesiles();
try
{
for (File f : fList) {
if (f.isFile()) {
attributes = Files.readAttributes(f.toPath(), BasicFileAttributes.class);
//System.out.println("File Name ="+f.getName());
fileSize= f.length();
size=String.valueOf(fileSize);
//System.out.println("File Size :: " + fileSize);
// System.out.println("File Size :: " + f);
System.out.println("File Name :: "+ f.getName()+"Size :: "+ size + "Full Path "+f.toString());
System.out.println("Creation Time ::"+attributes.creationTime());
csvOutput.write(f.getName());
csvOutput.write(attributes.creationTime().toString());
csvOutput.write(size);
csvOutput.write(f.toString());
csvOutput.endRecord();
} else if (f.isDirectory()) {
System.out.println(f);
listfiles(f.getAbsolutePath());
}
csvOutput.flush();
}
}
catch(IOException exception)
{
System.out.println("Exception handled when trying to get file " +
"attributes: " + exception.getMessage());
}
}
}
Thanks for giving great kind of information. Very useful and practical for me. Thanks for your excellent blog, nice work keep it up thanks for sharing the knowledge.
ReplyDeleteapi development company
Thanks for the helpful content . If you are looking a API Development so visit xettle.net site for the best company to provide fintech software.
ReplyDelete