How to get date picture created in java

后端 未结 5 2360
青春惊慌失措
青春惊慌失措 2021-02-20 16:26

I would like to extract the date a jpg file was created. Java has the lastModified method for the File object, but appears to provide no support for extracting the created date

5条回答
  •  北海茫月
    2021-02-20 17:24

    The code example below asks the user for a file path and then outputs the creation date and time:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Main {
    
        public static void main(final 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 /a "+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,15));
                }
                catch(StringIndexOutOfBoundsException se)
                {
                    System.out.println("File not found");
                }
                }
              catch (IOException ioe){ System.err.println(ioe); }
              catch (Throwable t) { t.printStackTrace();}
        }
    }
    

提交回复
热议问题