< prev index next >

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

Print this page




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.net.URI;
  29 import java.net.URL;
  30 import java.net.MalformedURLException;
  31 import java.net.URISyntaxException;

  32 import java.nio.file.FileSystems;
  33 import java.nio.file.Path;
  34 import java.security.SecureRandom;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import sun.security.action.GetPropertyAction;
  38 
  39 /**
  40  * An abstract representation of file and directory pathnames.
  41  *
  42  * <p> User interfaces and operating systems use system-dependent <em>pathname
  43  * strings</em> to name files and directories.  This class presents an
  44  * abstract, system-independent view of hierarchical pathnames.  An
  45  * <em>abstract pathname</em> has two components:
  46  *
  47  * <ol>
  48  * <li> An optional system-dependent <em>prefix</em> string,
  49  *      such as a disk-drive specifier, {@code "/"}&nbsp;for the UNIX root
  50  *      directory, or {@code "\\\\"}&nbsp;for a Microsoft Windows UNC pathname, and
  51  * <li> A sequence of zero or more string <em>names</em>.


1781      * particular root directory, then that directory will not appear in the
1782      * result.
1783      *
1784      * @return  An array of {@code File} objects denoting the available
1785      *          filesystem roots, or {@code null} if the set of roots could not
1786      *          be determined.  The array will be empty if there are no
1787      *          filesystem roots.
1788      *
1789      * @since  1.2
1790      * @see java.nio.file.FileStore
1791      */
1792     public static File[] listRoots() {
1793         return fs.listRoots();
1794     }
1795 
1796 
1797     /* -- Disk usage -- */
1798 
1799     /**
1800      * Returns the size of the partition <a href="#partName">named</a> by this
1801      * abstract pathname.


1802      *
1803      * @return  The size, in bytes, of the partition or {@code 0L} if this
1804      *          abstract pathname does not name a partition
1805      *
1806      * @throws  SecurityException
1807      *          If a security manager has been installed and it denies
1808      *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
1809      *          or its {@link SecurityManager#checkRead(String)} method denies
1810      *          read access to the file named by this abstract pathname
1811      *
1812      * @since  1.6

1813      */
1814     public long getTotalSpace() {
1815         SecurityManager sm = System.getSecurityManager();
1816         if (sm != null) {
1817             sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
1818             sm.checkRead(path);
1819         }
1820         if (isInvalid()) {
1821             return 0L;
1822         }
1823         return fs.getSpace(this, FileSystem.SPACE_TOTAL);

1824     }
1825 
1826     /**
1827      * Returns the number of unallocated bytes in the partition <a
1828      * href="#partName">named</a> by this abstract path name.


1829      *
1830      * <p> The returned number of unallocated bytes is a hint, but not
1831      * a guarantee, that it is possible to use most or any of these
1832      * bytes.  The number of unallocated bytes is most likely to be
1833      * accurate immediately after this call.  It is likely to be made
1834      * inaccurate by any external I/O operations including those made
1835      * on the system outside of this virtual machine.  This method
1836      * makes no guarantee that write operations to this file system
1837      * will succeed.
1838      *
1839      * @return  The number of unallocated bytes on the partition or {@code 0L}
1840      *          if the abstract pathname does not name a partition.  This
1841      *          value will be less than or equal to the total file system size
1842      *          returned by {@link #getTotalSpace}.
1843      *
1844      * @throws  SecurityException
1845      *          If a security manager has been installed and it denies
1846      *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
1847      *          or its {@link SecurityManager#checkRead(String)} method denies
1848      *          read access to the file named by this abstract pathname
1849      *
1850      * @since  1.6

1851      */
1852     public long getFreeSpace() {
1853         SecurityManager sm = System.getSecurityManager();
1854         if (sm != null) {
1855             sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
1856             sm.checkRead(path);
1857         }
1858         if (isInvalid()) {
1859             return 0L;
1860         }
1861         return fs.getSpace(this, FileSystem.SPACE_FREE);

1862     }
1863 
1864     /**
1865      * Returns the number of bytes available to this virtual machine on the
1866      * partition <a href="#partName">named</a> by this abstract pathname.  When
1867      * possible, this method checks for write permissions and other operating
1868      * system restrictions and will therefore usually provide a more accurate
1869      * estimate of how much new data can actually be written than {@link
1870      * #getFreeSpace}.


1871      *
1872      * <p> The returned number of available bytes is a hint, but not a
1873      * guarantee, that it is possible to use most or any of these bytes.  The
1874      * number of available bytes is most likely to be accurate immediately
1875      * after this call.  It is likely to be made inaccurate by any external
1876      * I/O operations including those made on the system outside of this
1877      * virtual machine.  This method makes no guarantee that write operations
1878      * to this file system will succeed.
1879      *
1880      * @return  The number of available bytes on the partition or {@code 0L}
1881      *          if the abstract pathname does not name a partition.  On
1882      *          systems where this information is not available, this method
1883      *          will be equivalent to a call to {@link #getFreeSpace}.
1884      *
1885      * @throws  SecurityException
1886      *          If a security manager has been installed and it denies
1887      *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
1888      *          or its {@link SecurityManager#checkRead(String)} method denies
1889      *          read access to the file named by this abstract pathname
1890      *
1891      * @since  1.6

1892      */
1893     public long getUsableSpace() {
1894         SecurityManager sm = System.getSecurityManager();
1895         if (sm != null) {
1896             sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
1897             sm.checkRead(path);
1898         }
1899         if (isInvalid()) {
1900             return 0L;
1901         }
1902         return fs.getSpace(this, FileSystem.SPACE_USABLE);

1903     }
1904 
1905     /* -- Temporary files -- */
1906 
1907     private static class TempDirectory {
1908         private TempDirectory() { }
1909 
1910         // temporary directory location
1911         private static final File tmpdir = new File(
1912                 GetPropertyAction.privilegedGetProperty("java.io.tmpdir"));
1913         static File location() {
1914             return tmpdir;
1915         }
1916 
1917         // file name generation
1918         private static final SecureRandom random = new SecureRandom();
1919         private static int shortenSubName(int subNameLength, int excess,
1920             int nameMin) {
1921             int newLength = Math.max(nameMin, subNameLength - excess);
1922             if (newLength < subNameLength) {




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.net.URI;
  29 import java.net.URL;
  30 import java.net.MalformedURLException;
  31 import java.net.URISyntaxException;
  32 import java.nio.file.FileStore;
  33 import java.nio.file.FileSystems;
  34 import java.nio.file.Path;
  35 import java.security.SecureRandom;
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 import sun.security.action.GetPropertyAction;
  39 
  40 /**
  41  * An abstract representation of file and directory pathnames.
  42  *
  43  * <p> User interfaces and operating systems use system-dependent <em>pathname
  44  * strings</em> to name files and directories.  This class presents an
  45  * abstract, system-independent view of hierarchical pathnames.  An
  46  * <em>abstract pathname</em> has two components:
  47  *
  48  * <ol>
  49  * <li> An optional system-dependent <em>prefix</em> string,
  50  *      such as a disk-drive specifier, {@code "/"}&nbsp;for the UNIX root
  51  *      directory, or {@code "\\\\"}&nbsp;for a Microsoft Windows UNC pathname, and
  52  * <li> A sequence of zero or more string <em>names</em>.


1782      * particular root directory, then that directory will not appear in the
1783      * result.
1784      *
1785      * @return  An array of {@code File} objects denoting the available
1786      *          filesystem roots, or {@code null} if the set of roots could not
1787      *          be determined.  The array will be empty if there are no
1788      *          filesystem roots.
1789      *
1790      * @since  1.2
1791      * @see java.nio.file.FileStore
1792      */
1793     public static File[] listRoots() {
1794         return fs.listRoots();
1795     }
1796 
1797 
1798     /* -- Disk usage -- */
1799 
1800     /**
1801      * Returns the size of the partition <a href="#partName">named</a> by this
1802      * abstract pathname. If the total number of bytes in the partition is
1803      * greater than {@link Long#MAX_VALUE}, then {@code Long.MAX_VALUE} will be
1804      * returned.
1805      *
1806      * @return  The size, in bytes, of the partition or {@code 0L} if this
1807      *          abstract pathname does not name a partition
1808      *
1809      * @throws  SecurityException
1810      *          If a security manager has been installed and it denies
1811      *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
1812      *          or its {@link SecurityManager#checkRead(String)} method denies
1813      *          read access to the file named by this abstract pathname
1814      *
1815      * @since  1.6
1816      * @see FileStore#getTotalSpace
1817      */
1818     public long getTotalSpace() {
1819         SecurityManager sm = System.getSecurityManager();
1820         if (sm != null) {
1821             sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
1822             sm.checkRead(path);
1823         }
1824         if (isInvalid()) {
1825             return 0L;
1826         }
1827         long space = fs.getSpace(this, FileSystem.SPACE_TOTAL);
1828         return space >= 0L ? space : Long.MAX_VALUE;
1829     }
1830 
1831     /**
1832      * Returns the number of unallocated bytes in the partition <a
1833      * href="#partName">named</a> by this abstract path name.  If the
1834      * number of unallocated bytes in the partition is greater than
1835      * {@link Long#MAX_VALUE}, then {@code Long.MAX_VALUE} will be returned.
1836      *
1837      * <p> The returned number of unallocated bytes is a hint, but not
1838      * a guarantee, that it is possible to use most or any of these
1839      * bytes.  The number of unallocated bytes is most likely to be
1840      * accurate immediately after this call.  It is likely to be made
1841      * inaccurate by any external I/O operations including those made
1842      * on the system outside of this virtual machine.  This method
1843      * makes no guarantee that write operations to this file system
1844      * will succeed.
1845      *
1846      * @return  The number of unallocated bytes on the partition or {@code 0L}
1847      *          if the abstract pathname does not name a partition.  This
1848      *          value will be less than or equal to the total file system size
1849      *          returned by {@link #getTotalSpace}.
1850      *
1851      * @throws  SecurityException
1852      *          If a security manager has been installed and it denies
1853      *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
1854      *          or its {@link SecurityManager#checkRead(String)} method denies
1855      *          read access to the file named by this abstract pathname
1856      *
1857      * @since  1.6
1858      * @see FileStore#getUnallocatedSpace
1859      */
1860     public long getFreeSpace() {
1861         SecurityManager sm = System.getSecurityManager();
1862         if (sm != null) {
1863             sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
1864             sm.checkRead(path);
1865         }
1866         if (isInvalid()) {
1867             return 0L;
1868         }
1869         long space = fs.getSpace(this, FileSystem.SPACE_FREE);
1870         return space >= 0L ? space : Long.MAX_VALUE;
1871     }
1872 
1873     /**
1874      * Returns the number of bytes available to this virtual machine on the
1875      * partition <a href="#partName">named</a> by this abstract pathname.  If
1876      * the number of available bytes in the partition is greater than
1877      * {@link Long#MAX_VALUE}, then {@code Long.MAX_VALUE} will be returned.
1878      * When possible, this method checks for write permissions and other
1879      * operating system restrictions and will therefore usually provide a more
1880      * accurate estimate of how much new data can actually be written than
1881      * {@link #getFreeSpace}.
1882      *
1883      * <p> The returned number of available bytes is a hint, but not a
1884      * guarantee, that it is possible to use most or any of these bytes.  The
1885      * number of available bytes is most likely to be accurate immediately
1886      * after this call.  It is likely to be made inaccurate by any external
1887      * I/O operations including those made on the system outside of this
1888      * virtual machine.  This method makes no guarantee that write operations
1889      * to this file system will succeed.
1890      *
1891      * @return  The number of available bytes on the partition or {@code 0L}
1892      *          if the abstract pathname does not name a partition.  On
1893      *          systems where this information is not available, this method
1894      *          will be equivalent to a call to {@link #getFreeSpace}.
1895      *
1896      * @throws  SecurityException
1897      *          If a security manager has been installed and it denies
1898      *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
1899      *          or its {@link SecurityManager#checkRead(String)} method denies
1900      *          read access to the file named by this abstract pathname
1901      *
1902      * @since  1.6
1903      * @see FileStore#getUsableSpace
1904      */
1905     public long getUsableSpace() {
1906         SecurityManager sm = System.getSecurityManager();
1907         if (sm != null) {
1908             sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
1909             sm.checkRead(path);
1910         }
1911         if (isInvalid()) {
1912             return 0L;
1913         }
1914         long space = fs.getSpace(this, FileSystem.SPACE_USABLE);
1915         return space >= 0L ? space : Long.MAX_VALUE;
1916     }
1917 
1918     /* -- Temporary files -- */
1919 
1920     private static class TempDirectory {
1921         private TempDirectory() { }
1922 
1923         // temporary directory location
1924         private static final File tmpdir = new File(
1925                 GetPropertyAction.privilegedGetProperty("java.io.tmpdir"));
1926         static File location() {
1927             return tmpdir;
1928         }
1929 
1930         // file name generation
1931         private static final SecureRandom random = new SecureRandom();
1932         private static int shortenSubName(int subNameLength, int excess,
1933             int nameMin) {
1934             int newLength = Math.max(nameMin, subNameLength - excess);
1935             if (newLength < subNameLength) {


< prev index next >