Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute Java Online

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class myClass {

	public static void main(String [] args) throws IOException {
        Path p = Paths.get("C:\\Users\\KalamLocal\\Desktop\\KT");
        File f = new File (p.toString());
        //System.out.println(f.list().length);
     // int i = f.list().length;
     // File Directory;
	//int noOfFiles = f.list().length;
        for (File file : f.listFiles()){
            int j=1;
             DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
     
  String fileName = new SimpleDateFormat("yyyyMMddHHmm'.txt'").format(new Date());
             File appendfile = new File(fileName);
//System.out.println("new file is created");
  FileWriter fr = new FileWriter(appendfile , true);
  BufferedWriter br = new BufferedWriter(fr);
        	System.out.println("ljlsjflsjljs");
       
        if (file.isDirectory()) {
        	if (file.list().length>0){

        		System.out.println("file is not empty");
        		File[] listOfFiles = file.listFiles();
        		for (int i = 0; i < listOfFiles.length; i++) {
        			System.out.println("File " + listOfFiles[i].getName());
   //System.out.println(listOfFiles[i].length());
   long inkb = listOfFiles[i].length() / 1024;
   long inmb = inkb /1024;
   String size = Long.toString(inmb);
   //System.out.println(inkb);
   //System.out.println(inmb);
 
  // SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
  //System.out.println("after format date is " + sdf.format(listOfFiles[i].lastModified()));
   //String strDate = sdf.format(sdf.format(listOfFiles[i].lastModified()));
   //String dateis = sdf.format(strDate);
  String strDate = dateFormat.format(listOfFiles[i].lastModified());
 
  br.write(String.format("%-20.20s" , listOfFiles[i].getName()));
  br.write("\t");
  br.write(String.format("%-15.15s", strDate));
  br.write("\t");
  br.write("\t");
  br.write(size);
  
  br.newLine();

  
  
  
        		}
        	}
        	else{
        		System.out.println("file is empty");
        		
        	}
        }
        br.newLine();
        br.write(j + "th Folder");
        br.newLine();
        br.newLine();
        br.close();
  fr.close();
        }
        	
	}
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.