Java Directory Check checkDir(String directory)

Here you can find the source of checkDir(String directory)

Description

Checks the given directory for its suffix, ('/' and '\', depending on the detected OS.

License

Open Source License

Parameter

Parameter Description
directory the directory whose suffix is to be checked

Return

the directory as a string, ensuring it ends with the correct character

Declaration

public static String checkDir(String directory) 

Method Source Code

//package com.java2s;
/**//from w w  w. java 2s  .c om
 * 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;

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

    /**
     * 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. checkDir(final String dirName)
  2. checkDir(final String name)
  3. checkDir(String dir)
  4. checkDir(String dir, boolean mustWrite)
  5. checkDir(String dirName)
  6. checkDir(String name)
  7. checkDir(String path)
  8. checkDir(String path)