< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  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


1503      * </ul>
1504      *
1505      * @param   path
1506      *          one path to the file
1507      * @param   path2
1508      *          the other path
1509      *
1510      * @return  {@code true} if, and only if, the two paths locate the same file
1511      *
1512      * @throws  IOException
1513      *          if an I/O error occurs
1514      * @throws  SecurityException
1515      *          In the case of the default provider, and a security manager is
1516      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
1517      *          method is invoked to check read access to both files.
1518      *
1519      * @see java.nio.file.attribute.BasicFileAttributes#fileKey
1520      */
1521     public static boolean isSameFile(Path path, Path path2) throws IOException {
1522         return provider(path).isSameFile(path, path2);














































































1523     }
1524 
1525     /**
1526      * Tells whether or not a file is considered <em>hidden</em>. The exact
1527      * definition of hidden is platform or provider dependent. On UNIX for
1528      * example a file is considered to be hidden if its name begins with a
1529      * period character ('.'). On Windows a file is considered hidden if it
1530      * isn't a directory and the DOS {@link DosFileAttributes#isHidden hidden}
1531      * attribute is set.
1532      *
1533      * <p> Depending on the implementation this method may require to access
1534      * the file system to determine if the file is considered hidden.
1535      *
1536      * @param   path
1537      *          the path to the file to test
1538      *
1539      * @return  {@code true} if the file is considered hidden
1540      *
1541      * @throws  IOException
1542      *          if an I/O error occurs


   1 /*
   2  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  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


1503      * </ul>
1504      *
1505      * @param   path
1506      *          one path to the file
1507      * @param   path2
1508      *          the other path
1509      *
1510      * @return  {@code true} if, and only if, the two paths locate the same file
1511      *
1512      * @throws  IOException
1513      *          if an I/O error occurs
1514      * @throws  SecurityException
1515      *          In the case of the default provider, and a security manager is
1516      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
1517      *          method is invoked to check read access to both files.
1518      *
1519      * @see java.nio.file.attribute.BasicFileAttributes#fileKey
1520      */
1521     public static boolean isSameFile(Path path, Path path2) throws IOException {
1522         return provider(path).isSameFile(path, path2);
1523     }
1524 
1525     /**
1526      * Tests if the content of two files is identical. This method extends the
1527      * {@link #isSameFile(java.nio.file.Path, java.nio.file.Path) isSameFile}
1528      * method to further compare the content of the two files.
1529      *
1530      * <p> It always returns true when path and path2:
1531      *
1532      * <ul>
1533      *     <li> point to the same object, even if the file does not exist;
1534      *     </li>
1535      *     <li> are equals as determined by
1536      * {@link #isSameFile(java.nio.file.Path, java.nio.file.Path) isSameFile(path, path2)}
1537      *     </li>
1538      * </ul>
1539      *
1540      * <p> If {@code isSameFile(path, path2)} returns false, this method will proceed
1541      * to read the files and compare them byte by byte to determine if they contain
1542      * the same content. It is assumed that the two files remain static during
1543      * the comparison process.
1544      *
1545      *
1546      * @param   path
1547      *          the path to a file
1548      * @param   path2
1549      *          the path to another file
1550      *
1551      * @return  a boolean indicating whether path and path2 locate the same file or
1552      *          their contents are identical.
1553      *
1554      * @throws  IOException
1555      *          if an I/O error occurs
1556      * @throws  SecurityException
1557      *          In the case of the default provider, and a security manager is
1558      *          installed, the {@link SecurityManager#checkRead(String) checkRead}
1559      *          method is invoked to check read access to both files.
1560      *
1561      * @since 11
1562      */
1563     public static boolean isSameContent(Path path, Path path2) throws IOException {
1564         if (isSameFile(path, path2)) {
1565             return true;
1566         }
1567 
1568         final long size = Files.size(path);
1569         if (size != Files.size(path2)) {
1570             return false;
1571         }
1572 
1573         try (SeekableByteChannel sbc = Files.newByteChannel(path);
1574             InputStream in = Channels.newInputStream(sbc);
1575             SeekableByteChannel sbc2 = Files.newByteChannel(path2);
1576             InputStream in2 = Channels.newInputStream(sbc2);) {
1577 
1578             byte[] buf = new byte[BUFFER_SIZE];
1579             byte[] buf2 = new byte[BUFFER_SIZE];
1580             int n, n2;
1581             while ((n = in.read(buf)) > 0) {
1582                 n2 = in2.read(buf2);
1583                 // unless the file is changed, n and n2 shall equal since in the previous
1584                 // step we've made sure the file sizes are equal
1585                 if (n != n2) {
1586                     return false;
1587                 }
1588 
1589                 if (n == BUFFER_SIZE) {
1590                     if (!Arrays.equals(buf, buf2)) {
1591                         return false;
1592                     }
1593                 } else {
1594                     if (!Arrays.equals(buf, 0, n, buf2, 0, n2)) {
1595                         return false;
1596                     }
1597                 }
1598             }
1599         }
1600         return true;
1601     }
1602 
1603     /**
1604      * Tells whether or not a file is considered <em>hidden</em>. The exact
1605      * definition of hidden is platform or provider dependent. On UNIX for
1606      * example a file is considered to be hidden if its name begins with a
1607      * period character ('.'). On Windows a file is considered hidden if it
1608      * isn't a directory and the DOS {@link DosFileAttributes#isHidden hidden}
1609      * attribute is set.
1610      *
1611      * <p> Depending on the implementation this method may require to access
1612      * the file system to determine if the file is considered hidden.
1613      *
1614      * @param   path
1615      *          the path to the file to test
1616      *
1617      * @return  {@code true} if the file is considered hidden
1618      *
1619      * @throws  IOException
1620      *          if an I/O error occurs


< prev index next >