Java read file creation time and last modified time

Source: Internet
Author: User

  1. Import Java.io.BufferedReader;
  2. Import Java.io.File;
  3. Import java.io.IOException;
  4. Import Java.io.InputStream;
  5. Import Java.io.InputStreamReader;
  6. Import Java.text.SimpleDateFormat;
  7. Import Java.util.Calendar;
  8. /**
  9. * Read file creation time and last modified time
  10. */
  11. Public class Readfiletime {
  12. public static void Main (string[] args) {
  13. Getcreatetime ();
  14. Getmodifiedtime_1 ();
  15. Getmodifiedtime_2 ();
  16. }
  17. /** 
  18. * Read File creation time
  19. */
  20. public static void Getcreatetime () {
  21. String FilePath = "C:\\Test.txt";
  22. String strtime = null;
  23. try {
  24. Process p = runtime.getruntime (). EXEC ("cmd/c dir"
  25. + FilePath
  26. + "/tc");
  27. InputStream is = P.getinputstream ();
  28. BufferedReader br = new BufferedReader (new InputStreamReader (IS));
  29. String Line;
  30. While (line = Br.readline ()) = null) {
  31. if (Line.endswith (". txt")) {
  32. Strtime = line.substring (0,17);
  33. Break ;
  34. }
  35. }
  36. } catch (IOException e) {
  37. E.printstacktrace ();
  38. }
  39. System.out.println ("creation time" + strtime);
  40. //output: Creation time 2009-08-17 10:21
  41. }
  42. /** 
  43. * Method of reading file modification time 1
  44. */
  45. @SuppressWarnings ("deprecation")
  46. public static void Getmodifiedtime_1 () {
  47. File F = new file ("C:\\Test.txt");
  48. Calendar cal = Calendar.getinstance ();
  49. Long time = f.lastmodified ();
  50. Cal.settimeinmillis (time);
  51. //Here The Tolocalstring () method is not recommended, but can still be output
  52. System.out.println ("Modified time [1]" + cal.gettime (). toLocaleString ());
  53. //output: Modification time [1] 2009-8-17 10:32:38
  54. }
  55. /** 
  56. * Method of Reading modification time 2
  57. */
  58. public static void getmodifiedtime_2 () {
  59. File F = new file ("C:\\Test.txt");
  60. Calendar cal = Calendar.getinstance ();
  61. Long time = f.lastmodified ();
  62. SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
  63. Cal.settimeinmillis (time);
  64. SYSTEM.OUT.PRINTLN ("Modification time [2]" + Formatter.format (Cal.gettime ()));
  65. //output: Modification time [2] 2009-08-17 10:32:38
  66. }
  67. }

Java read file creation time and last modified time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.