Here you can find the source of createHiddenFile(String filePath)
Parameter | Description |
---|---|
filePath | path to file |
public static void createHiddenFile(String filePath)
//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; } }