Java Path Create nio createHiddenFile(String filePath)

Here you can find the source of createHiddenFile(String filePath)

Description

Creates a hidden file based on the directory and file name given.

License

Open Source License

Parameter

Parameter Description
filePath path to file

Declaration

public static void createHiddenFile(String filePath) 

Method Source Code

//package com.java2s;
/**//from   w  w w .  j  av a2  s  .  c  o m
 * An xkcd comic-viewer made in Java.
 * Copyright (C) 2017 dcarpinelli
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Paths;

public class Main {
    public static final int OTHER = 0, WINDOWS = 1, MACINTOSH = 2, LINUX = 3;

    /**
     * Creates a hidden file based on the directory and file name given. Will
     * check for suffixes to prevent errors.
     *
     * @param filePath path to file
     */
    public static void createHiddenFile(String filePath) {
        String directory = filePath.substring(0, filePath.lastIndexOf(File.separator) + 1);
        directory = checkDir(directory);
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
        // CREATE ANY MISSING DIRECTORIES
        new File(directory).mkdirs();
        try {
            File file = null;
            switch (detectOS()) {
            case WINDOWS:
                // CREATE FILE
                file = new File(directory + fileName);
                file.createNewFile();
                // SET FILE TO HIDDEN
                Files.setAttribute(Paths.get(directory, fileName), "dos:hidden", true);
                break;
            case MACINTOSH:
            case LINUX:
                // CHECK FOR HIDDEN ATTRIBUTE
                if (!fileName.startsWith(".")) {
                    fileName = "." + fileName;
                }
                // CREATE FILE
                file = new File(directory + fileName);
                file.createNewFile();
                break;
            default:
                System.err.println("[FileUtil : createHiddenFile] Detected OS is not supported.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Checks the given directory for its suffix, ('/' and '\', depending on the
     * detected OS. If no suffix is found, it is added to prevent errors in
     * other methods.
     *
     * @param directory the directory whose suffix is to be checked
     * @return the directory as a string, ensuring it ends with the correct character
     */
    public static String checkDir(String directory) {
        switch (detectOS()) {
        case OTHER:
            System.err.println("[FileUtil : checkDir] Detected OS is not supported.");
            return null;
        default:
            if (!directory.endsWith(File.separator)) {
                directory += File.separator;
            }
            return directory;
        }
    }

    /**
     * Checks the operating system FilUtil is being used by. Only specifies
     * Windows, Macintosh, and Linux.
     *
     * @return integer in relation to detected operating system
     */
    public static int detectOS() {
        int osState;
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("windows")) {
            osState = WINDOWS;
        } else if (os.contains("mac")) {
            osState = MACINTOSH;
        } else if (os.contains("linux")) {
            osState = LINUX;
        } else {
            osState = OTHER;
        }
        return osState;
    }
}

Related

  1. createFolder(String workspacePath)
  2. createFoldersIfNecessary(String workspacePath)
  3. createFullURI(String val, Path parent)
  4. createGlobMatcher(final Path path)
  5. createHeader(Path newFile)
  6. createIncrNonExistentFilename(final Path parent, final String prefix, final String suffix)
  7. createJarFile(Path directory, String name, Optional manifest, Class[] classesToAdd, Map filesToAdd)
  8. createJSONFileIfNotExists(Path path)
  9. createLanguageStructure(final String lang, final Path docRootFolder)