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:com.wordnik.swagger.codegen.util.FileUtil.java

public static void copyDirectory(File srcPath, File dstPath) {
    if (srcPath.isDirectory()) {
        if (!dstPath.exists()) {
            dstPath.mkdir();
        }// w w w  .ja v  a2 s.c  o  m

        String files[] = srcPath.list();
        for (int i = 0; i < files.length; i++) {
            copyDirectory(new File(srcPath, files[i]), new File(dstPath, files[i]));
        }
    } else {
        if (!srcPath.exists()) {
            throw new CodeGenerationException("Source folder does not exist");
        } else {
            try {
                InputStream in = new FileInputStream(srcPath);
                OutputStream out = new FileOutputStream(dstPath);

                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw new CodeGenerationException("Copy directory operation failed");
            }
        }
    }
}

From source file:com.chinarewards.gwt.license.util.FileUploadServlet.java

/**
 * @param uploadRootName//from  www  .  j a va2s  . co m
 *            
 * 
 * */
private static String getUploadPath(HttpServletRequest request, String uploadRootName) {
    String realPath = request.getSession().getServletContext().getRealPath("/");
    String uploadPath = null;
    // System.out.println("============realPath:" + realPath);
    int rootIndex = realPath.indexOf("jboss-5.1.0.GA");
    if (rootIndex < 0) {
        rootIndex = realPath.indexOf("war");
    }

    if (rootIndex < 0) {
        return null;
    } else {
        realPath = realPath.substring(0, rootIndex);

        uploadPath = realPath + uploadRootName;
        // System.out.println("============uploadPath:" + uploadPath);
        File myFilePath = new File(uploadPath);
        if (!myFilePath.exists()) {
            myFilePath.mkdir();
            System.out.println("" + myFilePath);
        }
    }
    return uploadPath;
}

From source file:com.ikon.util.FileUtils.java

/**
 * Creates a temporal and unique directory
 * //from   w ww .j  av  a 2 s.c o  m
 * @throws IOException If something fails.
 */
public static File createTempDir() throws IOException {
    File tmpFile = File.createTempFile("okm", null);

    if (!tmpFile.delete())
        throw new IOException();
    if (!tmpFile.mkdir())
        throw new IOException();
    return tmpFile;
}

From source file:com.kotcrab.vis.editor.Log.java

private static void prepareLogFile() {
    File logDirectory = new File(App.APP_FOLDER_PATH, "logs");
    logDirectory.mkdir();

    SimpleDateFormat fileDateFormat = new SimpleDateFormat("yy-MM-dd");
    String fileName = fileDateFormat.format(new Date());
    String fileNameYearMonth = fileName.substring(0, 5);

    File[] files = logDirectory.listFiles();

    if (files != null) {
        //we are deleting files that are not from current month
        for (File f : files) {
            if (f.getName().contains(fileNameYearMonth) == false)
                f.delete();//  w ww.  java 2s .  com
        }
    }

    try {
        logFile = new File(logDirectory, "viseditor " + fileName + ".txt");
        boolean fileExists = logFile.exists();
        logFile.createNewFile();
        logFileStream = new BufferedOutputStream(new FileOutputStream(logFile, true));
        if (fileExists)
            logFileStream.write(System.lineSeparator().getBytes());
    } catch (IOException e) {
        exception(e);
    }

    info("VisEditor " + App.VERSION + " (version code: " + App.VERSION_CODE + ")");
    info("Started: " + fileName);
}

From source file:msi.gama.doc.util.PrepareEnv.java

public static void prepareDocumentation(boolean online) throws IOException {
    // - Deletes every generated folders      
    // - Creates every folders when they do not exist

    File genFolder = new File(Constants.GEN_FOLDER);
    File testFolder = new File(Constants.TEST_FOLDER);

    if (genFolder.exists()) {
        FileUtils.deleteDirectory(genFolder);
    }//from w ww.  j  av  a2s .c o m
    if (testFolder.exists()) {
        FileUtils.deleteDirectory(testFolder);
    }

    genFolder.mkdir();
    new File(Constants.JAVA2XML_FOLDER).mkdirs();
    new File(Constants.XML2WIKI_FOLDER).mkdirs();
    new File(Constants.WIKI2WIKI_FOLDER).mkdirs();
    new File(Constants.HTML2XML_FOLDER).mkdirs();
    new File(Constants.PDF_FOLDER).mkdirs();
    new File(Constants.TEST_FOLDER).mkdirs();
    new File(Constants.TOC_GEN_FOLDER).mkdir();

    copyPythonTemplate();
}

From source file:com.nullwire.ExceptionHandler.java

/**
 * Search for stack trace files.//from w ww .  ja v  a  2 s.  c o  m
 * @return
 */
private static String[] searchForStackTraces() {
    if (stackTraceFileList != null) {
        return stackTraceFileList;
    }
    File dir = new File(G.FILES_PATH + "/");
    // Try to create the files folder if it doesn't exist
    dir.mkdir();
    // Filter for ".stacktrace" files
    FilenameFilter filter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.endsWith(".stacktrace");
        }
    };
    return (stackTraceFileList = dir.list(filter));
}

From source file:uploadProcess.java

public static boolean encrypt(String path, FileItemStream item, String patientID) {
    try {/*from w  w  w.j  ava  2  s .  co m*/
        File mainFolder = new File(path + File.separator + "Encrypt");
        if (!mainFolder.exists()) {
            mainFolder.mkdir();
        }
        File patientFolder = new File(mainFolder + File.separator + patientID);
        if (!patientFolder.exists()) {
            patientFolder.mkdir();
        }
        String ukey = GetKey.getPatientKey(patientID);
        InputStream fis = item.openStream();

        FileOutputStream fos = new FileOutputStream(
                patientFolder.getAbsolutePath() + File.separator + item.getName());
        byte[] k = ukey.getBytes();
        SecretKeySpec key = new SecretKeySpec(k, "AES");
        System.out.println(key);
        Cipher enc = Cipher.getInstance("AES");
        enc.init(Cipher.ENCRYPT_MODE, key);
        CipherOutputStream cos = new CipherOutputStream(fos, enc);
        byte[] buf = new byte[1024];
        int read;
        while ((read = fis.read(buf)) != -1) {
            cos.write(buf, 0, read);
        }
        fis.close();
        fos.flush();
        cos.close();

        //Upload File to cloud
        DropboxUpload upload = new DropboxUpload();
        upload.uploadFile(patientFolder, item.getName(), StoragePath.getDropboxDir() + patientID);
        DeleteDirectory.delete(patientFolder);

        return true;
    } catch (Exception e) {
        System.out.println("Error: " + e);
    }
    return false;
}

From source file:eu.scape_project.tool.toolwrapper.core.utils.Utils.java

/**
 * Method useful to create a temporary directory identified by a given name
 * //from   www .  jav  a2s  .  c  om
 * @param name
 *            name of the temporary directory to be created
 * @return the {@link File} object that identifies the created temporary
 *         directory or null if any exception occurs
 * */
public static File createTemporaryDirectory(String name) {
    File tempDir = null;
    try {
        tempDir = File.createTempFile(name, "");
        if (!(tempDir.delete() && tempDir.mkdir())) {
            tempDir = null;
            log.error("Error creating temp folder");
        }
    } catch (IOException e) {
        log.error("Error while creating temporary folder (\"" + name + "\")");
    }
    return tempDir;
}

From source file:outfox.dict.contest.util.FileUtils.java

/**
 * ?NOSurl//from  w  w w  . j  a  va2s  .co  m
 * tongkn
 * @param bytes
 * @return
 */
public static String uploadFile2Nos(byte[] bytes, String fileName) throws Exception {
    if (bytes == null) {
        return null;
    }
    HttpResponse response = null;
    String result = null;
    try {
        //web ??
        String filedir = "tmpfile";
        File dir = new File(filedir);
        if (!dir.exists()) {
            dir.mkdir();
        }
        //            FileBody bin = new FileBody(FileUtils.getFileFromBytes(
        //                    bytes, filedir +"/file-"+ System.currentTimeMillis()+"-"+fileName));
        //ByteArrayBody
        ByteArrayBody bin = new ByteArrayBody(bytes, fileName);
        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("file", bin);
        reqEntity.addPart("video", new StringBody("false", Charset.forName("UTF-8")));
        //            reqEntity.addPart("contentType", new StringBody("audio/mpeg", Charset.forName("UTF-8")));
        response = HttpToolKit.getInstance().doPost(ContestConsts.NOS_UPLOAD_Interface, reqEntity);
        if (response != null) {
            String jsonString = EntityUtils.toString(response.getEntity());
            JSONObject json = JSON.parseObject(jsonString);
            if ("success".equals(json.getString("msg"))) {
                return json.getString("url");
            }
        }
    } catch (Exception e) {
        LOG.error("FileUtils.uploadFile2Nos(bytes) error...", e);
        throw new Exception();
    } finally {
        HttpToolKit.closeQuiet(response);
    }
    return result;
}

From source file:azkaban.jobtype.connectors.teradata.TeraDataWalletInitializer.java

/**
 * As of TDCH 1.4.1, integration with Teradata wallet only works with hadoop jar command line command.
 * This is mainly because TDCH depends on the behavior of hadoop jar command line which extracts jar file
 * into hadoop tmp folder./* ww w.j a v  a  2s  . c  om*/
 *
 * This method will extract tdchJarfile and place it into temporary folder, and also add jvm shutdown hook
 * to delete the directory when JVM shuts down.
 * @param tdchJarFile TDCH jar file.
 */
public static void initialize(File tmpDir, File tdchJarFile) {
    synchronized (TeraDataWalletInitializer.class) {
        if (tdchJarExtractedDir != null) {
            return;
        }

        if (tdchJarFile == null) {
            throw new IllegalArgumentException("TDCH jar file cannot be null.");
        }
        if (!tdchJarFile.exists()) {
            throw new IllegalArgumentException(
                    "TDCH jar file does not exist. " + tdchJarFile.getAbsolutePath());
        }
        try {
            //Extract TDCH jar.
            File unJarDir = createUnjarDir(
                    new File(tmpDir.getAbsolutePath() + File.separator + UNJAR_DIR_NAME));
            JarFile jar = new JarFile(tdchJarFile);
            Enumeration<JarEntry> enumEntries = jar.entries();

            while (enumEntries.hasMoreElements()) {
                JarEntry srcFile = enumEntries.nextElement();
                File destFile = new File(unJarDir + File.separator + srcFile.getName());
                if (srcFile.isDirectory()) { // if its a directory, create it
                    destFile.mkdir();
                    continue;
                }

                InputStream is = jar.getInputStream(srcFile);
                BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(destFile));
                IOUtils.copy(is, os);
                close(os);
                close(is);
            }
            jar.close();
            tdchJarExtractedDir = unJarDir;
        } catch (IOException e) {
            throw new RuntimeException("Failed while extracting TDCH jar file.", e);
        }
    }
    logger.info("TDCH jar has been extracted into directory: " + tdchJarExtractedDir.getAbsolutePath());
}