954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Output a file's type and created date

Hi,

I was wondering if someone could help me. I am using java SWT and am trying to output a list of files and its properties to the screen. I have a file browser popping up and when i select a directory and click ok it outputs the path and list of files in that directory. However, i also want to output the type and created time of each file in that directory. i have an array that holds all the files but i am having trouble doing this. can anyone please help me.

Thanks in advanced,

scoobie

here is the code i am using:

DirectoryDialog dialog = new DirectoryDialog(shell);
dialog.setText("Browse For Folder");
dialog.setFilterPath("c:\\");

String res = dialog.open();

File path = new File(res);

File[] files;
files = path.listFiles();

int i;
int count = 1;
for(i = 0; i < files.length; i++)
{
	File filename = files[i];
	long size = filename.length()/1000;
	
        System.out.println(count + "  Filename: " + filename + 
        "\n" + "Size:  " + size + " KB\n" + 
        "Path:  " + path + "\n\n");
	
count++;	        			        	
}

i++;
scoobie
Newbie Poster
20 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

hi friends..
i searched many forums to get file creation date
i didnt get the solution but i found one program which execute
dos command and gets the output of that.
so using that program i developed a program to get the file creation Date & Time also

//getCreationDate.java 
import java.io.*;
import java.lang.*;
 
public class getCreationDate {
 
public static void main (String args[]){
  try {
     // get runtime environment and execute child process
     Runtime systemShell = Runtime.getRuntime();
	 BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
	 System.out.println("Enter filename: ");
	 String fname=(String)br1.readLine();
     Process output = systemShell.exec("cmd /c dir "+fname);
	  // open reader to get output from process
     BufferedReader br = new BufferedReader (new InputStreamReader(output.getInputStream()));
	 
     String out="";
	 String line = null;
     
	 int step=1;
      while((line = br.readLine()) != null ) 
       {
		  if(step==6)
		 {
		 out=line;
		 }
		  step++;
		  }          // display process output
     
	 try{
	 out=out.replaceAll(" ","");
	 System.out.println("CreationDate: "+out.substring(0,10));
	 System.out.println("CreationTime: "+out.substring(10,16)+"m");
	 }
	 catch(StringIndexOutOfBoundsException se)
	 {
		 System.out.println("File not found");
	 }
     }
   catch (IOException ioe){ System.err.println(ioe); }
   catch (Throwable t) { t.printStackTrace();}
}
}
santhu538
Newbie Poster
1 post since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

This is very nice.
If you wrap filename in "" you can also handle windows directories and files with spaces.

Like this:
Process output = systemShell.exec("cmd /c dir \""+fname + "\"");

Bjørn

bjornna
Newbie Poster
1 post since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

hey..
this is really very useful..
keep up the good work..!

mani.mani
Newbie Poster
2 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

You can get the last modification date of a file with File.lastModified(). That long can then be used to set the time in a Date or Calendar instance. Date prints nicely with toString() or you can use a DateFormatter. Avoiding OS calls has the advantage of not restricting your code to only work on Windows.

For the file type, I'm guessing that you mean the filename extension rather than a "magic number". You can use something like this:

String name = myFile.getName();
String fileType = name.substring(name.lastIndexOf(".") + 1); // Assumes extension.

Well, there's not much to that. Maybe that's not what you wanted.

Hope that helps.

glynnst
Newbie Poster
1 post since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Java is designed for use on deifferent platforms. Unix does not store creation date. That's why you can't get file creation date in java.
P.S.: hello from Ukraine.

Murdok@i
Newbie Poster
1 post since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You