Example usage for java.io File mkdir

List of usage examples for java.io File mkdir

Introduction

In this page you can find the example usage for java.io File mkdir.

Prototype

public boolean mkdir() 

Source Link

Document

Creates the directory named by this abstract pathname.

Usage

From source file:hudson.plugins.jobConfigHistory.JobConfigHistorySaveableListener.java

/**  
 * Copy file from one location to another.
 * @param src file to copy from//from   w  w w. jav a  2 s .co  m
 * @param dest file to copy to
 */
public static void copyFolder(File src, File dest) throws IOException {

    if (src.isDirectory()) {

        //if directory not exists, create it
        if (!dest.exists()) {
            dest.mkdir();
        }

        //list all the directory contents
        String files[] = src.list();

        for (String file : files) {
            //construct the src and dest file structure
            File srcFile = new File(src, file);
            File destFile = new File(dest, file);
            //recursive copy
            copyFolder(srcFile, destFile);
        }

    } else {
        //if file, then copy it
        //Use bytes stream to support all file types
        if (!dest.exists()) {
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest);

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes 
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }

            in.close();
            out.close();
        }
    }
}

From source file:net.sourceforge.atunes.kernel.modules.plugins.PluginsHandler.java

/**
 * Return path to core plugins folder, which is where application is
 * installed (working directory)/*from   w w w  .  jav  a2s  . c om*/
 * 
 * @return
 */
private static String getCorePluginsFolder() {
    String workingDirectory = SystemProperties.getWorkingDirectory();
    String pluginsFolder = StringUtils.getString(workingDirectory, SystemProperties.FILE_SEPARATOR,
            Constants.CORE_PLUGINS_DIR);
    File pluginFile = new File(pluginsFolder);
    if (!pluginFile.exists()) {
        if (!pluginFile.mkdir()) {
            return workingDirectory;
        }
    }
    return pluginsFolder;
}

From source file:net.sourceforge.atunes.kernel.modules.plugins.PluginsHandler.java

/**
 * Return path to plugins folder, which is inside user config folder.
 * //w ww .ja  v  a 2 s .  com
 * @return the plugins folder
 */
private static String getUserPluginsFolder() {
    String userConfigFolder = SystemProperties.getUserConfigFolder(Kernel.DEBUG);
    String pluginsFolder = StringUtils.getString(userConfigFolder, SystemProperties.FILE_SEPARATOR,
            Constants.PLUGINS_DIR);
    File pluginFile = new File(pluginsFolder);
    if (!pluginFile.exists()) {
        if (!pluginFile.mkdir()) {
            return userConfigFolder;
        }
    }
    return pluginsFolder;
}

From source file:org.openjira.jira.utils.LoadImageAsync.java

public static void saveImageToFile(String url, Bitmap bm) {
    try {//ww w  . ja v  a  2  s  . c  o m
        File root = Environment.getExternalStorageDirectory();
        if (root.canWrite()) {
            File dir = new File(root, "openjiracache");
            dir.mkdir();
            File file = new File(dir,
                    url.replace("/", "_").replace(":", "-").replace("?", "_").replace("=", "-"));

            FileOutputStream os = new FileOutputStream(file);
            bm.compress(CompressFormat.PNG, 5, os);
            // Log.v(LOGTAG, "Saved bitmap in " + file.getAbsolutePath());
        }
    } catch (Throwable e) {
        Log.e(LOGTAG, "Could not write file " + e.getMessage());
    }
}

From source file:com.ecofactor.qa.automation.platform.util.FileUtil.java

/**
 * Un zip a file to a destination folder.
 * @param zipFile the zip file/* ww  w  . ja  v a2 s .  co m*/
 * @param outputFolder the output folder
 */
public static void unZipFile(final String zipFile, final String outputFolder) {

    LogUtil.setLogString("UnZip the File", true);
    final byte[] buffer = new byte[1024];
    int len;
    try {
        // create output directory is not exists
        final File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }
        // get the zip file content
        final ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        final StringBuilder outFolderPath = new StringBuilder();
        final StringBuilder fileLogPath = new StringBuilder();
        ZipEntry zipEntry;
        while ((zipEntry = zis.getNextEntry()) != null) {
            final String fileName = zipEntry.getName();
            final File newFile = new File(
                    outFolderPath.append(outputFolder).append(File.separator).append(fileName).toString());
            LogUtil.setLogString(
                    fileLogPath.append("file unzip : ").append(newFile.getAbsoluteFile()).toString(), true);
            // create all non exists folders
            // else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();
            final FileOutputStream fos = new FileOutputStream(newFile);

            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            fileLogPath.setLength(0);
            outFolderPath.setLength(0);
        }

        zis.closeEntry();
        zis.close();

        LogUtil.setLogString("Done", true);

    } catch (IOException ex) {
        LOGGER.error("Error in unzip file", ex);
    }
}

From source file:com.eryansky.common.utils.io.FileUtil.java

/**
 * ??/*from w  ww  .ja va2  s .co  m*/
 * 
 * @param Directorypath
 */
public static void createDirectory(String Directorypath) {
    File file = new File(Directorypath);
    if (!file.exists()) {
        file.mkdir();
        file.mkdirs();
    }
}

From source file:com.splout.db.common.TestUtils.java

/**
 * Returns a DNode instance if, after a maximum of X trials, we can find a port to bind it to.
 * The configuration passed by instance might have been modified accordingly.
 */// w  ww  .ja va2 s .  co  m
public static DNode getTestDNode(final SploutConfiguration testConfig, final IDNodeHandler handler,
        final String dataFolder, boolean deleteDataFolder) throws Throwable {
    final AtomicReference<DNode> reference = new AtomicReference<DNode>();
    testConfig.setProperty(DNodeProperties.DATA_FOLDER, dataFolder);
    if (deleteDataFolder) {
        File file = new File(dataFolder);
        if (file.exists()) {
            FileUtils.deleteDirectory(file);
        }
        file.mkdir();
    }
    testConfig.setProperty(FetcherProperties.TEMP_DIR, "fetcher-" + dataFolder);
    File fetcherTmp = new File("fetcher-" + dataFolder);
    if (fetcherTmp.exists()) {
        FileUtils.deleteDirectory(fetcherTmp);
    }
    fetcherTmp.mkdir();
    CatchAndRetry dNodeInit = new CatchAndRetry(TTransportException.class, 50) {

        @Override
        public void businessLogic() throws Throwable {
            DNode dNode = new DNode(testConfig, handler);
            dNode.init();
            reference.set(dNode);
        }

        @Override
        public void retryLogic() {
            testConfig.setProperty(DNodeProperties.PORT, testConfig.getInt(DNodeProperties.PORT) + 1);
        }
    };
    dNodeInit.catchAndRetry();
    return reference.get();
}

From source file:com.mgmtp.perfload.perfalyzer.util.IoUtilities.java

/**
 * Unzips a zip file./*from www  . jav a 2 s.com*/
 *
 * @param zip
 *       the zip file
 * @param destDir
 *       the destination directory (will be created if non-existent)
 */
public static void unzip(final ZipFile zip, final File destDir) throws IOException {
    if (!destDir.exists()) {
        destDir.mkdir();
    }

    for (Enumeration<? extends ZipEntry> zipEntryEnum = zip.entries(); zipEntryEnum.hasMoreElements();) {
        ZipEntry zipEntry = zipEntryEnum.nextElement();
        extractEntry(zip, zipEntry, destDir);
    }
}

From source file:fredboat.audio.MusicPersistenceHandler.java

public static void handlePreShutdown(int code) {
    File dir = new File("music_persistence");
    if (!dir.exists()) {
        dir.mkdir();
    }//from  w  ww  .j  av  a 2 s  .c  om
    HashMap<String, GuildPlayer> reg = PlayerRegistry.getRegistry();

    boolean isUpdate = code == ExitCodes.EXIT_CODE_UPDATE;
    boolean isRestart = code == ExitCodes.EXIT_CODE_RESTART;

    for (String gId : reg.keySet()) {
        try {
            GuildPlayer player = reg.get(gId);

            if (!player.isPlaying()) {
                continue;//Nothing to see here
            }

            String msg;

            if (isUpdate) {
                msg = I18n.get(player.getGuild()).getString("shutdownUpdating");
            } else if (isRestart) {
                msg = I18n.get(player.getGuild()).getString("shutdownRestarting");
            } else {
                msg = I18n.get(player.getGuild()).getString("shutdownIndef");
            }

            player.getActiveTextChannel().sendMessage(msg).queue();

            JSONObject data = new JSONObject();
            data.put("vc", player.getUserCurrentVoiceChannel(player.getGuild().getSelfMember()).getId());
            data.put("tc", player.getActiveTextChannel().getId());
            data.put("isPaused", player.isPaused());
            data.put("volume", Float.toString(player.getVolume()));
            data.put("repeatMode", player.getRepeatMode());
            data.put("shuffle", player.isShuffle());

            if (player.getPlayingTrack() != null) {
                data.put("position", player.getPlayingTrack().getEffectivePosition());
            }

            ArrayList<JSONObject> identifiers = new ArrayList<>();

            for (AudioTrackContext atc : player.getRemainingTracks()) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                AbstractPlayer.getPlayerManager().encodeTrack(new MessageOutput(baos), atc.getTrack());

                JSONObject ident = new JSONObject()
                        .put("message", Base64.encodeBase64String(baos.toByteArray()))
                        .put("user", atc.getMember().getUser().getId());

                if (atc instanceof SplitAudioTrackContext) {
                    JSONObject split = new JSONObject();
                    SplitAudioTrackContext c = (SplitAudioTrackContext) atc;
                    split.put("title", c.getEffectiveTitle()).put("startPos", c.getStartPosition())
                            .put("endPos", c.getStartPosition() + c.getEffectiveDuration());

                    ident.put("split", split);
                }

                identifiers.add(ident);
            }

            data.put("sources", identifiers);

            try {
                FileUtils.writeStringToFile(new File(dir, gId), data.toString(), Charset.forName("UTF-8"));
            } catch (IOException ex) {
                player.getActiveTextChannel()
                        .sendMessage(MessageFormat.format(
                                I18n.get(player.getGuild()).getString("shutdownPersistenceFail"),
                                ex.getMessage()))
                        .queue();
            }
        } catch (Exception ex) {
            log.error("Error when saving persistence file", ex);
        }
    }
}

From source file:org.openjira.jira.utils.LoadImageAsync.java

public static Bitmap getImageFromFile(String url) {
    try {/*from   www  . j a v a 2  s. com*/
        File root = Environment.getExternalStorageDirectory();
        if (root.canWrite()) {
            File dir = new File(root, "openjiracache");
            dir.mkdir();
            File file = new File(dir,
                    url.replace("/", "_").replace(":", "-").replace("?", "_").replace("=", "-"));
            if (file.exists() && file.length() > 0) {
                FileInputStream is = new FileInputStream(file);
                Bitmap bm = BitmapFactory.decodeStream(is);
                // Log.v(LOGTAG, "Loaded file from " +
                // file.getAbsolutePath());
                WeakReference<Bitmap> ref = new WeakReference<Bitmap>(bm);
                cachedBitmaps.put(url, ref);
                return bm;
            }
        }
    } catch (Throwable e) {
        Log.e(LOGTAG, "Could not read file " + e.getMessage());
    }
    return null;
}