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++;

Recommended Answers

All 6 Replies

Member Avatar for iamthwee

Maybe this

commented: nice link +3

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();}
}
}

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

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.