0

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++;
7
Contributors
6
Replies
9
Views
10 Years
Discussion Span
Last Post by Murdok@i
0

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

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.

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.

Edited by Murdok@i: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.