1 /*
2 * Copyright (c) 2007, 2019, 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
637 * if the array contains an attribute that cannot be set atomically
638 * when creating the file
639 * @throws FileAlreadyExistsException
640 * if a file of that name already exists
641 * <i>(optional specific exception)</i>
642 * @throws IOException
643 * if an I/O error occurs or the parent directory does not exist
644 * @throws SecurityException
645 * In the case of the default provider, and a security manager is
646 * installed, the {@link SecurityManager#checkWrite(String) checkWrite}
647 * method is invoked to check write access to the new file.
648 */
649 public static Path createFile(Path path, FileAttribute<?>... attrs)
650 throws IOException
651 {
652 newByteChannel(path, DEFAULT_CREATE_OPTIONS, attrs).close();
653 return path;
654 }
655
656 /**
657 * Creates a new directory. The check for the existence of the file and the
658 * creation of the directory if it does not exist are a single operation
659 * that is atomic with respect to all other filesystem activities that might
660 * affect the directory. The {@link #createDirectories createDirectories}
661 * method should be used where it is required to create all nonexistent
662 * parent directories first.
663 *
664 * <p> The {@code attrs} parameter is optional {@link FileAttribute
665 * file-attributes} to set atomically when creating the directory. Each
666 * attribute is identified by its {@link FileAttribute#name name}. If more
667 * than one attribute of the same name is included in the array then all but
668 * the last occurrence is ignored.
669 *
670 * @param dir
671 * the directory to create
672 * @param attrs
673 * an optional list of file attributes to set atomically when
674 * creating the directory
675 *
676 * @return the directory
677 *
678 * @throws UnsupportedOperationException
679 * if the array contains an attribute that cannot be set atomically
680 * when creating the directory
681 * @throws FileAlreadyExistsException
682 * if a directory could not otherwise be created because a file of
683 * that name already exists <i>(optional specific exception)</i>
684 * @throws IOException
685 * if an I/O error occurs or the parent directory does not exist
686 * @throws SecurityException
687 * In the case of the default provider, and a security manager is
688 * installed, the {@link SecurityManager#checkWrite(String) checkWrite}
689 * method is invoked to check write access to the new directory.
690 */
691 public static Path createDirectory(Path dir, FileAttribute<?>... attrs)
692 throws IOException
693 {
694 provider(dir).createDirectory(dir, attrs);
695 return dir;
696 }
697
698 /**
699 * Creates a directory by creating all nonexistent parent directories first.
700 * Unlike the {@link #createDirectory createDirectory} method, an exception
701 * is not thrown if the directory could not be created because it already
702 * exists.
703 *
|
1 /*
2 * Copyright (c) 2007, 2020, 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
637 * if the array contains an attribute that cannot be set atomically
638 * when creating the file
639 * @throws FileAlreadyExistsException
640 * if a file of that name already exists
641 * <i>(optional specific exception)</i>
642 * @throws IOException
643 * if an I/O error occurs or the parent directory does not exist
644 * @throws SecurityException
645 * In the case of the default provider, and a security manager is
646 * installed, the {@link SecurityManager#checkWrite(String) checkWrite}
647 * method is invoked to check write access to the new file.
648 */
649 public static Path createFile(Path path, FileAttribute<?>... attrs)
650 throws IOException
651 {
652 newByteChannel(path, DEFAULT_CREATE_OPTIONS, attrs).close();
653 return path;
654 }
655
656 /**
657 * Creates a new directory, failing if the directory already exists. The
658 * check for the existence of the directory and the creation of the
659 * directory if it does not exist are a single operation that is atomic
660 * with respect to all other filesystem activities that might affect the
661 * directory. The {@link #createDirectories createDirectories} method
662 * should be used where it is required to create all nonexistent parent
663 * directories first.
664 *
665 * <p> The {@code attrs} parameter is optional {@link FileAttribute
666 * file-attributes} to set atomically when creating the directory. Each
667 * attribute is identified by its {@link FileAttribute#name name}. If more
668 * than one attribute of the same name is included in the array then all but
669 * the last occurrence is ignored.
670 *
671 * @param dir
672 * the directory to create
673 * @param attrs
674 * an optional list of file attributes to set atomically when
675 * creating the directory
676 *
677 * @return the directory
678 *
679 * @throws UnsupportedOperationException
680 * if the array contains an attribute that cannot be set atomically
681 * when creating the directory
682 * @throws FileAlreadyExistsException
683 * if the directory could not otherwise be created because
684 * {@code dir} already exists <i>(optional specific exception)</i>
685 * @throws IOException
686 * if an I/O error occurs or the parent directory does not exist
687 * @throws SecurityException
688 * In the case of the default provider, and a security manager is
689 * installed, the {@link SecurityManager#checkWrite(String) checkWrite}
690 * method is invoked to check write access to the new directory.
691 */
692 public static Path createDirectory(Path dir, FileAttribute<?>... attrs)
693 throws IOException
694 {
695 provider(dir).createDirectory(dir, attrs);
696 return dir;
697 }
698
699 /**
700 * Creates a directory by creating all nonexistent parent directories first.
701 * Unlike the {@link #createDirectory createDirectory} method, an exception
702 * is not thrown if the directory could not be created because it already
703 * exists.
704 *
|