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 } 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 1553 * installed, the {@link SecurityManager#checkRead(String) checkRead} 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 private 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 } 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 * Finds and returns the position of the first mismatched byte in the content 1538 * of two files, or {@code -1L} if there is no mismatch. The position will be 1539 * in the inclusive range of {@code 0L} up to the size (in bytes) of the 1540 * smaller file. 1541 * 1542 * <p> Two files are considered to match if they satisfy one of the following 1543 * conditions: 1544 * <ul> 1545 * <li> The two paths locate the {@link #isSameFile(Path, Path) same file}, 1546 * even if two {@link Path#equals(Object) equal} paths locate a file 1547 * does not exist, or </li> 1548 * <li> The two files are the same size, and every byte in the first file 1549 * is identical to the corresponding byte in the second file. </li> 1550 * </ul> 1551 * 1552 * <p> Otherwise there is a mismatch between the two files and the value 1553 * returned by this method is: 1554 * <ul> 1555 * <li> The position of the first mismatched byte, or </li> 1556 * <li> The size of the smaller file (in bytes) when the files are different 1557 * sizes and every byte of the smaller file is identical to the 1558 * corresponding byte of the larger file. </li> 1559 * </ul> 1560 * 1561 * <p> This method may not be atomic with respect to other file system 1562 * operations. If the file system and files remain static then this method 1563 * is <i>reflexive</i> (for {@code Path} {@code f}, {@code mismatch(f,f)} 1564 * returns {@code -1L}), and <i>symmetric</i> (for two {@code Paths} {@code 1565 * f} and {@code g}, {@code mismatch(f,g)} will return the same value as 1566 * {@code mismatch(g,f)}). 1567 * 1568 * @param path 1569 * the path to the first file 1570 * @param path2 1571 * the path to the second file 1572 * 1573 * @return the position of the first mismatch or {@code -1L} if no mismatch 1574 * 1575 * @throws IOException 1576 * if an I/O error occurs 1577 * @throws SecurityException 1578 * In the case of the default provider, and a security manager is 1579 * installed, the {@link SecurityManager#checkRead(String) checkRead} 1580 * method is invoked to check read access to both files. 1581 * 1582 * @since 12 1583 */ 1584 public static long mismatch(Path path, Path path2) throws IOException { 1585 if (isSameFile(path, path2)) { 1586 return -1; 1587 } 1588 byte[] buffer1 = new byte[Files.BUFFER_SIZE]; 1589 byte[] buffer2 = new byte[Files.BUFFER_SIZE]; 1590 try (InputStream in1 = Files.newInputStream(path); 1591 InputStream in2 = Files.newInputStream(path2);) { 1592 int totalRead = 0; 1593 while (true) { 1594 int nRead1 = in1.readNBytes(buffer1, 0, Files.BUFFER_SIZE); 1595 int nRead2 = in2.readNBytes(buffer2, 0, Files.BUFFER_SIZE); 1596 if (nRead1 == 0 && nRead2 == 0) { 1597 // both files reached EOF 1598 return -1; 1599 } else if (nRead1 == 0 || nRead2 == 0) { 1600 // one but not both files reached EOF 1601 return totalRead; 1602 } else if (nRead1 != nRead2) { 1603 // there's always a mismatch when nRead1 != nRead2 1604 return totalRead + Arrays.mismatch(buffer1, 0, nRead1, buffer2, 0, nRead2); 1605 } else { 1606 int i = Arrays.mismatch(buffer1, 0, nRead1, buffer2, 0, nRead1); 1607 if (i > -1) { 1608 return totalRead + i; 1609 } 1610 if (nRead1 < Files.BUFFER_SIZE) { 1611 // we've reached the end of the files, but found no mismatch 1612 return -1; 1613 } 1614 totalRead += nRead1; 1615 } 1616 } 1617 } 1618 } 1619 1620 /** 1621 * Tells whether or not a file is considered <em>hidden</em>. The exact 1622 * definition of hidden is platform or provider dependent. On UNIX for 1623 * example a file is considered to be hidden if its name begins with a 1624 * period character ('.'). On Windows a file is considered hidden if it 1625 * isn't a directory and the DOS {@link DosFileAttributes#isHidden hidden} 1626 * attribute is set. 1627 * 1628 * <p> Depending on the implementation this method may require to access 1629 * the file system to determine if the file is considered hidden. 1630 * 1631 * @param path 1632 * the path to the file to test 1633 * 1634 * @return {@code true} if the file is considered hidden 1635 * 1636 * @throws IOException 1637 * if an I/O error occurs 1638 * @throws SecurityException 1639 * In the case of the default provider, and a security manager is 1640 * installed, the {@link SecurityManager#checkRead(String) checkRead} 2872 * @throws SecurityException 2873 * If the security manager denies access to the starting file. 2874 * In the case of the default provider, the {@link 2875 * SecurityManager#checkRead(String) checkRead} method is invoked 2876 * to check read access to the directory. 2877 * @throws IOException 2878 * if an I/O error is thrown by a visitor method 2879 */ 2880 public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor) 2881 throws IOException 2882 { 2883 return walkFileTree(start, 2884 EnumSet.noneOf(FileVisitOption.class), 2885 Integer.MAX_VALUE, 2886 visitor); 2887 } 2888 2889 2890 // -- Utility methods for simple usages -- 2891 2892 2893 /** 2894 * Opens a file for reading, returning a {@code BufferedReader} that may be 2895 * used to read text from the file in an efficient manner. Bytes from the 2896 * file are decoded into characters using the specified charset. Reading 2897 * commences at the beginning of the file. 2898 * 2899 * <p> The {@code Reader} methods that read from the file throw {@code 2900 * IOException} if a malformed or unmappable byte sequence is read. 2901 * 2902 * @param path 2903 * the path to the file 2904 * @param cs 2905 * the charset to use for decoding 2906 * 2907 * @return a new buffered reader, with default buffer size, to read text 2908 * from the file 2909 * 2910 * @throws IOException 2911 * if an I/O error occurs opening the file |