In particular, check the return value of the Java file operation related methods

Source: Internet
Author: User



Today, there is a problem with dog blood, a function in the development environment is not a problem, but in the production environment error.



The code is as follows:


...
                 File tmpFile = new File(fileTmpPath);
File newFileTarget = new File(filePath);
tmpFile.renameTo(newFileTarget);
// Modify the permissions of the new file
FileManageHelper.chmod(newFileTarget);
....


The last error message indicates that the Execute chmod command failed, but this code is not a problem in the development environment.



A closer look at the reasons for discovering the JDK Renameto method is described below:


 /**
     * Renames the file denoted by this abstract pathname.
     *
     * <p> Many aspects of the behavior of this method are inherently
     * platform-dependent: The rename operation might not be able to move a
     * file from one filesystem to another, it might not be atomic, and it
     * might not succeed if a file with the destination abstract pathname
     * already exists.  The return value should always be checked to make sure
     * that the rename operation was successful.
     *
     * <p> Note that the {@link java.nio.file.Files} class defines the {@link
     * java.nio.file.Files#move move} method to move or rename a file in a
     * platform independent manner.
     *
     * @param  dest  The new abstract pathname for the named file
     *
     * @return  <code>true</code> if and only if the renaming succeeded;
     *          <code>false</code> otherwise
     *
     * @throws  SecurityException
     *          If a security manager exists and its <code>{@link
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
     *          method denies write access to either the old or new pathnames
     *
     * @throws  NullPointerException
     *          If parameter <code>dest</code> is <code>null</code>
     */





The rename operation might is able to move a file from one filesystem to another



That is, if the file is to move files from one file system to another file system it is possible to fail, just as the development environment on Tmpfile and Newfiletarget in the same file system, and in the production environment due to the HA scenario, these two files are in different file systems.



Lesson: Be sure to check the return values of file related operations, such as Setlastmodified, Setreadonly, setwritable, setreadable, setexecutable, CreateNewFile, delete, mkdir, Mkdirs






In particular, check the return value of the Java file operation related methods


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.