< prev index next >

src/java.base/share/classes/java/nio/file/Files.java

Print this page




  74 import java.util.Spliterator;
  75 import java.util.Spliterators;
  76 import java.util.function.BiPredicate;
  77 import java.util.stream.Stream;
  78 import java.util.stream.StreamSupport;
  79 
  80 import sun.nio.ch.FileChannelImpl;
  81 import sun.nio.fs.AbstractFileSystemProvider;
  82 
  83 /**
  84  * This class consists exclusively of static methods that operate on files,
  85  * directories, or other types of files.
  86  *
  87  * <p> In most cases, the methods defined here will delegate to the associated
  88  * file system provider to perform the file operations.
  89  *
  90  * @since 1.7
  91  */
  92 
  93 public final class Files {



  94     private Files() { }
  95 
  96     /**
  97      * Returns the {@code FileSystemProvider} to delegate to.
  98      */
  99     private static FileSystemProvider provider(Path path) {
 100         return path.getFileSystem().provider();
 101     }
 102 
 103     /**
 104      * Convert a Closeable to a Runnable by converting checked IOException
 105      * to UncheckedIOException
 106      */
 107     private static Runnable asUncheckedRunnable(Closeable c) {
 108         return () -> {
 109             try {
 110                 c.close();
 111             } catch (IOException e) {
 112                 throw new UncheckedIOException(e);
 113             }


1513      * @param   path
1514      *          one path to the file
1515      * @param   path2
1516      *          the other path
1517      *
1518      * @return  {@code true} if, and only if, the two paths locate the same file
1519      *
1520      * @throws  IOException
1521      *          if an I/O error occurs
1522      * @throws  SecurityException
1523      *          In the case of the default provider, and a security manager is
1524      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
1525      *          method is invoked to check read access to both files.
1526      *
1527      * @see java.nio.file.attribute.BasicFileAttributes#fileKey
1528      */
1529     public static boolean isSameFile(Path path, Path path2) throws IOException {
1530         return provider(path).isSameFile(path, path2);
1531     }
1532 















































1533     /**
1534      * Tells whether or not a file is considered <em>hidden</em>. The exact
1535      * definition of hidden is platform or provider dependent. On UNIX for
1536      * example a file is considered to be hidden if its name begins with a
1537      * period character ('.'). On Windows a file is considered hidden if it
1538      * isn't a directory and the DOS {@link DosFileAttributes#isHidden hidden}
1539      * attribute is set.
1540      *
1541      * <p> Depending on the implementation this method may require to access
1542      * the file system to determine if the file is considered hidden.
1543      *
1544      * @param   path
1545      *          the path to the file to test
1546      *
1547      * @return  {@code true} if the file is considered hidden
1548      *
1549      * @throws  IOException
1550      *          if an I/O error occurs
1551      * @throws  SecurityException
1552      *          In the case of the default provider, and a security manager is


2785      * @throws  SecurityException
2786      *          If the security manager denies access to the starting file.
2787      *          In the case of the default provider, the {@link
2788      *          SecurityManager#checkRead(String) checkRead} method is invoked
2789      *          to check read access to the directory.
2790      * @throws  IOException
2791      *          if an I/O error is thrown by a visitor method
2792      */
2793     public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor)
2794         throws IOException
2795     {
2796         return walkFileTree(start,
2797                             EnumSet.noneOf(FileVisitOption.class),
2798                             Integer.MAX_VALUE,
2799                             visitor);
2800     }
2801 
2802 
2803     // -- Utility methods for simple usages --
2804 
2805     // buffer size used for reading and writing
2806     private static final int BUFFER_SIZE = 8192;
2807 
2808     /**
2809      * Opens a file for reading, returning a {@code BufferedReader} that may be
2810      * used to read text from the file in an efficient manner. Bytes from the
2811      * file are decoded into characters using the specified charset. Reading
2812      * commences at the beginning of the file.
2813      *
2814      * <p> The {@code Reader} methods that read from the file throw {@code
2815      * IOException} if a malformed or unmappable byte sequence is read.
2816      *
2817      * @param   path
2818      *          the path to the file
2819      * @param   cs
2820      *          the charset to use for decoding
2821      *
2822      * @return  a new buffered reader, with default buffer size, to read text
2823      *          from the file
2824      *
2825      * @throws  IOException
2826      *          if an I/O error occurs opening the file




  74 import java.util.Spliterator;
  75 import java.util.Spliterators;
  76 import java.util.function.BiPredicate;
  77 import java.util.stream.Stream;
  78 import java.util.stream.StreamSupport;
  79 
  80 import sun.nio.ch.FileChannelImpl;
  81 import sun.nio.fs.AbstractFileSystemProvider;
  82 
  83 /**
  84  * This class consists exclusively of static methods that operate on files,
  85  * directories, or other types of files.
  86  *
  87  * <p> In most cases, the methods defined here will delegate to the associated
  88  * file system provider to perform the file operations.
  89  *
  90  * @since 1.7
  91  */
  92 
  93 public final class Files {
  94     // buffer size used for reading and writing
  95     static final int BUFFER_SIZE = 8192;
  96 
  97     private Files() { }
  98 
  99     /**
 100      * Returns the {@code FileSystemProvider} to delegate to.
 101      */
 102     private static FileSystemProvider provider(Path path) {
 103         return path.getFileSystem().provider();
 104     }
 105 
 106     /**
 107      * Convert a Closeable to a Runnable by converting checked IOException
 108      * to UncheckedIOException
 109      */
 110     private static Runnable asUncheckedRunnable(Closeable c) {
 111         return () -> {
 112             try {
 113                 c.close();
 114             } catch (IOException e) {
 115                 throw new UncheckedIOException(e);
 116             }


1516      * @param   path
1517      *          one path to the file
1518      * @param   path2
1519      *          the other path
1520      *
1521      * @return  {@code true} if, and only if, the two paths locate the same file
1522      *
1523      * @throws  IOException
1524      *          if an I/O error occurs
1525      * @throws  SecurityException
1526      *          In the case of the default provider, and a security manager is
1527      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
1528      *          method is invoked to check read access to both files.
1529      *
1530      * @see java.nio.file.attribute.BasicFileAttributes#fileKey
1531      */
1532     public static boolean isSameFile(Path path, Path path2) throws IOException {
1533         return provider(path).isSameFile(path, path2);
1534     }
1535 
1536 
1537     /**
1538      * Finds and returns the index of the first mismatched byte in the content
1539      * of two files, or returns -1 if the files are equal.
1540      *
1541      * <p> Two files are considered equal if they satisfy one of the following
1542      * conditions:
1543      * <ul>
1544      *   <li> {@link #isSameFile(Path, Path) isSameFile(path, path2)} returns true,</li>
1545      *   <li> the files are the same length, and every byte of the first file equals
1546      *   the corresponding byte of the second file.</li>
1547      * </ul>
1548      *
1549      * <p>Otherwise, there is a mismatch between the two files. This method returns
1550      * a mismatch value, computed as follows:
1551      * <ul>
1552      *   <li> the index of the first position in the files at which mismatched bytes
1553      *   are found, or</li>
1554      *   <li> the length of the smaller file, if the files are of unequal length,
1555      *   and if every byte of the smaller file equals the corresponding byte of
1556      *   the larger file.</li>
1557      * </ul>
1558      *
1559      * <p>This method may not be atomic with respect to other file system operations.
1560      *
1561      * @param   path
1562      *          the path to the first file
1563      * @param   path2
1564      *          the path to the second file
1565      *
1566      * @return  the index of the first mismatch between the two files, or {@code -1}
1567      *          if there is no mismatch.
1568      *
1569      * @throws  IOException
1570      *          if an I/O error occurs, or path or path2 does not exist unless
1571      *          they are the same object
1572      * @throws  SecurityException
1573      *          In the case of the default provider, and a security manager is
1574      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
1575      *          method is invoked to check read access to both files.
1576      * @since 12
1577      *
1578      */
1579     public static long mismatch(Path path, Path path2) throws IOException {
1580         return FilesMismatch.mismatch(path, path2);
1581     }
1582 
1583     /**
1584      * Tells whether or not a file is considered <em>hidden</em>. The exact
1585      * definition of hidden is platform or provider dependent. On UNIX for
1586      * example a file is considered to be hidden if its name begins with a
1587      * period character ('.'). On Windows a file is considered hidden if it
1588      * isn't a directory and the DOS {@link DosFileAttributes#isHidden hidden}
1589      * attribute is set.
1590      *
1591      * <p> Depending on the implementation this method may require to access
1592      * the file system to determine if the file is considered hidden.
1593      *
1594      * @param   path
1595      *          the path to the file to test
1596      *
1597      * @return  {@code true} if the file is considered hidden
1598      *
1599      * @throws  IOException
1600      *          if an I/O error occurs
1601      * @throws  SecurityException
1602      *          In the case of the default provider, and a security manager is


2835      * @throws  SecurityException
2836      *          If the security manager denies access to the starting file.
2837      *          In the case of the default provider, the {@link
2838      *          SecurityManager#checkRead(String) checkRead} method is invoked
2839      *          to check read access to the directory.
2840      * @throws  IOException
2841      *          if an I/O error is thrown by a visitor method
2842      */
2843     public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor)
2844         throws IOException
2845     {
2846         return walkFileTree(start,
2847                             EnumSet.noneOf(FileVisitOption.class),
2848                             Integer.MAX_VALUE,
2849                             visitor);
2850     }
2851 
2852 
2853     // -- Utility methods for simple usages --
2854 


2855 
2856     /**
2857      * Opens a file for reading, returning a {@code BufferedReader} that may be
2858      * used to read text from the file in an efficient manner. Bytes from the
2859      * file are decoded into characters using the specified charset. Reading
2860      * commences at the beginning of the file.
2861      *
2862      * <p> The {@code Reader} methods that read from the file throw {@code
2863      * IOException} if a malformed or unmappable byte sequence is read.
2864      *
2865      * @param   path
2866      *          the path to the file
2867      * @param   cs
2868      *          the charset to use for decoding
2869      *
2870      * @return  a new buffered reader, with default buffer size, to read text
2871      *          from the file
2872      *
2873      * @throws  IOException
2874      *          if an I/O error occurs opening the file


< prev index next >