942,789 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 26860
  • Java RSS
Aug 3rd, 2006
0

Output a file's type and created date

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  1. DirectoryDialog dialog = new DirectoryDialog(shell);
  2. dialog.setText("Browse For Folder");
  3. dialog.setFilterPath("c:\\");
  4.  
  5. String res = dialog.open();
  6.  
  7. File path = new File(res);
  8.  
  9. File[] files;
  10. files = path.listFiles();
  11.  
  12. int i;
  13. int count = 1;
  14. for(i = 0; i < files.length; i++)
  15. {
  16. File filename = files[i];
  17. long size = filename.length()/1000;
  18.  
  19. System.out.println(count + " Filename: " + filename +
  20. "\n" + "Size: " + size + " KB\n" +
  21. "Path: " + path + "\n\n");
  22.  
  23. count++;
  24. }
  25.  
  26. i++;
Last edited by scoobie; Aug 3rd, 2006 at 9:01 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scoobie is offline Offline
20 posts
since Jan 2005
Aug 3rd, 2006
1

Re: Output a file's type and created date

Maybe this
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 2nd, 2008
0

Re: Output a file's type and created date

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
Java Syntax (Toggle Plain Text)
  1. //getCreationDate.java
  2. import java.io.*;
  3. import java.lang.*;
  4.  
  5. public class getCreationDate {
  6.  
  7. public static void main (String args[]){
  8. try {
  9. // get runtime environment and execute child process
  10. Runtime systemShell = Runtime.getRuntime();
  11. BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
  12. System.out.println("Enter filename: ");
  13. String fname=(String)br1.readLine();
  14. Process output = systemShell.exec("cmd /c dir "+fname);
  15. // open reader to get output from process
  16. BufferedReader br = new BufferedReader (new InputStreamReader(output.getInputStream()));
  17.  
  18. String out="";
  19. String line = null;
  20.  
  21. int step=1;
  22. while((line = br.readLine()) != null )
  23. {
  24. if(step==6)
  25. {
  26. out=line;
  27. }
  28. step++;
  29. } // display process output
  30.  
  31. try{
  32. out=out.replaceAll(" ","");
  33. System.out.println("CreationDate: "+out.substring(0,10));
  34. System.out.println("CreationTime: "+out.substring(10,16)+"m");
  35. }
  36. catch(StringIndexOutOfBoundsException se)
  37. {
  38. System.out.println("File not found");
  39. }
  40. }
  41. catch (IOException ioe){ System.err.println(ioe); }
  42. catch (Throwable t) { t.printStackTrace();}
  43. }
  44. }
Last edited by santhu538; Feb 2nd, 2008 at 3:42 am. Reason: timepass
Reputation Points: 10
Solved Threads: 0
Newbie Poster
santhu538 is offline Offline
1 posts
since Feb 2008
Jul 3rd, 2009
0

Re: Output a file's type and created date

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bjornna is offline Offline
1 posts
since Jul 2009
May 27th, 2010
0
Re: Output a file's type and created date
hey..
this is really very useful..
keep up the good work..!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mani.mani is offline Offline
2 posts
since May 2010
Jun 29th, 2010
0

OS independent.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
glynnst is offline Offline
1 posts
since Jun 2010
Jun 2nd, 2011
0
Re: Output a file's type and created date
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.
Last edited by Murdok@i; Jun 2nd, 2011 at 6:11 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Murdok@i is offline Offline
1 posts
since Jun 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: How i can apply blinking in chat program..??
Next Thread in Java Forum Timeline: java.lang.NoSuchMethodError: main ??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC