< prev index next >

src/java.base/share/classes/java/io/File.java

Print this page




 917      * last modified.
 918      *
 919      * <p> Where it is required to distinguish an I/O exception from the case
 920      * where {@code 0L} is returned, or where several attributes of the
 921      * same file are required at the same time, or where the time of last
 922      * access or the creation time are required, then the {@link
 923      * java.nio.file.Files#readAttributes(Path,Class,LinkOption[])
 924      * Files.readAttributes} method may be used.
 925      *
 926      * @apiNote
 927      * While the unit of time of the return value is milliseconds,
 928      * the granularity of the value depends on the underlying
 929      * file system and may be larger.  For example, some
 930      * file systems use time stamps in units of seconds.
 931      *
 932      * @return  A <code>long</code> value representing the time the file was
 933      *          last modified, measured in milliseconds since the epoch
 934      *          (00:00:00 GMT, January 1, 1970), or <code>0L</code> if the
 935      *          file does not exist or if an I/O error occurs; the value may
 936      *          be negative in which case its absolute value indicates the
 937      *          number of milliseconds before the epoch


 938      *
 939      * @throws  SecurityException
 940      *          If a security manager exists and its {@link
 941      *          java.lang.SecurityManager#checkRead(java.lang.String)}
 942      *          method denies read access to the file
 943      */
 944     public long lastModified() {
 945         SecurityManager security = System.getSecurityManager();
 946         if (security != null) {
 947             security.checkRead(path);
 948         }
 949         if (isInvalid()) {
 950             return 0L;
 951         }
 952         return fs.getLastModifiedTime(this);











 953     }
 954 
 955     /**
 956      * Returns the length of the file denoted by this abstract pathname.
 957      * The return value is unspecified if this pathname denotes a directory.
 958      *
 959      * <p> Where it is required to distinguish an I/O exception from the case
 960      * that {@code 0L} is returned, or where several attributes of the same file
 961      * are required at the same time, then the {@link
 962      * java.nio.file.Files#readAttributes(Path,Class,LinkOption[])
 963      * Files.readAttributes} method may be used.
 964      *
 965      * @return  The length, in bytes, of the file denoted by this abstract
 966      *          pathname, or <code>0L</code> if the file does not exist.  Some
 967      *          operating systems may return <code>0L</code> for pathnames
 968      *          denoting system-dependent entities such as devices or pipes.
 969      *
 970      * @throws  SecurityException
 971      *          If a security manager exists and its {@link
 972      *          java.lang.SecurityManager#checkRead(java.lang.String)}




 917      * last modified.
 918      *
 919      * <p> Where it is required to distinguish an I/O exception from the case
 920      * where {@code 0L} is returned, or where several attributes of the
 921      * same file are required at the same time, or where the time of last
 922      * access or the creation time are required, then the {@link
 923      * java.nio.file.Files#readAttributes(Path,Class,LinkOption[])
 924      * Files.readAttributes} method may be used.
 925      *
 926      * @apiNote
 927      * While the unit of time of the return value is milliseconds,
 928      * the granularity of the value depends on the underlying
 929      * file system and may be larger.  For example, some
 930      * file systems use time stamps in units of seconds.
 931      *
 932      * @return  A <code>long</code> value representing the time the file was
 933      *          last modified, measured in milliseconds since the epoch
 934      *          (00:00:00 GMT, January 1, 1970), or <code>0L</code> if the
 935      *          file does not exist or if an I/O error occurs; the value may
 936      *          be negative in which case its absolute value indicates the
 937      *          number of milliseconds before the epoch; if the last-modified
 938      *          time is the epoch, the value will be unity (<code>1</code>)
 939      *          so as to avoid ambiguity with the case of a non-existent file
 940      *
 941      * @throws  SecurityException
 942      *          If a security manager exists and its {@link
 943      *          java.lang.SecurityManager#checkRead(java.lang.String)}
 944      *          method denies read access to the file
 945      */
 946     public long lastModified() {
 947         SecurityManager security = System.getSecurityManager();
 948         if (security != null) {
 949             security.checkRead(path);
 950         }
 951         if (isInvalid()) {
 952             return 0L;
 953         }
 954         long lastModifiedTime = fs.getLastModifiedTime(this);
 955 
 956         //
 957         // In the infinitesimally probable event that the last-modified time
 958         // of the file is the epoch (zero), return unity instead so as to avoid
 959         // ambiguity with the case of a non-existent file which returns zero.
 960         //
 961         if (lastModifiedTime == 0) {
 962             lastModifiedTime = 1;
 963         }
 964 
 965         return lastModifiedTime;
 966     }
 967 
 968     /**
 969      * Returns the length of the file denoted by this abstract pathname.
 970      * The return value is unspecified if this pathname denotes a directory.
 971      *
 972      * <p> Where it is required to distinguish an I/O exception from the case
 973      * that {@code 0L} is returned, or where several attributes of the same file
 974      * are required at the same time, then the {@link
 975      * java.nio.file.Files#readAttributes(Path,Class,LinkOption[])
 976      * Files.readAttributes} method may be used.
 977      *
 978      * @return  The length, in bytes, of the file denoted by this abstract
 979      *          pathname, or <code>0L</code> if the file does not exist.  Some
 980      *          operating systems may return <code>0L</code> for pathnames
 981      *          denoting system-dependent entities such as devices or pipes.
 982      *
 983      * @throws  SecurityException
 984      *          If a security manager exists and its {@link
 985      *          java.lang.SecurityManager#checkRead(java.lang.String)}


< prev index next >