Example usage for java.io File mkdirs

List of usage examples for java.io File mkdirs

Introduction

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

Prototype

public boolean mkdirs() 

Source Link

Document

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.

Usage

From source file:Main.java

public static void saveImage(String imagePath, byte[] buffer) throws IOException {
    File f = new File(imagePath);
    if (f.exists()) {
        return;/*from w  w w  . jav a  2  s.  c om*/
    } else {
        File parentFile = f.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        f.createNewFile();
        FileOutputStream fos = new FileOutputStream(imagePath);
        fos.write(buffer);
        fos.flush();
        fos.close();
    }
}

From source file:Main.java

public static void writeUrlToFileNIO(String urlToRead, String folderToWrite, String fileName)
        throws MalformedURLException, IOException {
    URL urlIn = new URL(urlToRead);
    File folderOut = Paths.get(folderToWrite).toFile();
    if (!(folderOut.exists() || folderOut.mkdirs())) {
        throw new RuntimeException("could not create folder " + folderToWrite);
    }/*from w w w .j  av  a2 s .  c o  m*/
    Path pathOut = Paths.get(folderToWrite, fileName);
    try (ReadableByteChannel in = Channels.newChannel(new BufferedInputStream(urlIn.openStream()));
            WritableByteChannel out = Files.newByteChannel(pathOut, CREATE, WRITE);) {
        transfer(in, out);
    }
}

From source file:Main.java

public static void unzip(File zipFile, File targetDirectory) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    try {// w w w  .j a  va 2  s .com
        ZipEntry ze;
        int count;
        byte[] buffer = new byte[8192];
        while ((ze = zis.getNextEntry()) != null) {
            File file = new File(targetDirectory, ze.getName());
            File dir = ze.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs())
                throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath());
            if (ze.isDirectory())
                continue;
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
        }
    } finally {
        zis.close();
    }
}

From source file:io.pivotal.strepsirrhini.chaosloris.docs.MarkdownWriterResolver.java

private static void createDirectoriesIfNecessary(File outputFile) {
    File parent = outputFile.getParentFile();
    if (!parent.isDirectory() && !parent.mkdirs()) {
        throw new IllegalStateException("Failed to create directory '" + parent + "'");
    }/*from   w  ww  .  java2  s . c o  m*/
}

From source file:com.davis.crs.CreateCannedDataSet.java

/**
 * Write data objects to json file.//from   w ww.  j a va  2s. co m
 *
 * @param dataHolders the data holders
 * @param fileName    the file name
 * @throws IOException the io exception
 */
public static void writeDataObjectsToJsonFile(ArrayList<CRSEndpointResponse> dataHolders, String fileName)
        throws IOException {
    System.out.println("Size of the CRSEndpointResponse Array = " + dataHolders.size());
    File file = new File(outputDir);

    if (!file.exists()) {
        file.mkdirs();
    }

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(dataHolders);
    File file1 = new File(outputDir + "/" + fileName);

    FileUtils.writeStringToFile(file1, json);
}

From source file:edu.uci.ics.asterix.test.metadata.MetadataTest.java

@BeforeClass
public static void setUp() throws Exception {
    System.setProperty(GlobalConfig.CONFIG_FILE_PROPERTY, TEST_CONFIG_FILE_NAME);
    File outdir = new File(PATH_ACTUAL);
    outdir.mkdirs();

    AsterixPropertiesAccessor apa = new AsterixPropertiesAccessor();
    txnProperties = new AsterixTransactionProperties(apa);

    deleteTransactionLogs();/*from   w w  w  .j  av  a 2 s  . c  om*/

    AsterixHyracksIntegrationUtil.init();
}

From source file:Main.java

private static boolean checkFsWritable() {
    String directoryName = Environment.getExternalStorageDirectory().toString() + "/DCIM";
    File directory = new File(directoryName);
    if (!directory.isDirectory()) {
        if (!directory.mkdirs()) {
            return false;
        }/*from  www . j a v  a  2s .c o m*/
    }
    File f = new File(directoryName, ".probe");
    try {
        if (f.exists()) {
            f.delete();
        }
        if (!f.createNewFile()) {
            return false;
        }
        f.delete();
        return true;
    } catch (IOException ex) {
        return false;
    }
}

From source file:com.netflix.spinnaker.clouddriver.appengine.artifacts.StorageUtils.java

public static void writeStreamToFile(InputStream sourceStream, File target) throws IOException {
    File parent = target.getParentFile();
    if (!parent.exists()) {
        parent.mkdirs();
    }/*from  www .  j av a  2 s  . c  om*/
    OutputStream targetStream = new FileOutputStream(target);
    IOUtils.copy(sourceStream, targetStream);
    targetStream.close();
}

From source file:Main.java

public static Uri saveBitmapToSDCard(Bitmap bitmap, String title) {
    File appDir = new File(Environment.getExternalStorageDirectory(), "Gank");
    if (!appDir.exists()) {
        appDir.mkdirs();
    }//from  ww  w  .j av a  2  s . c  o m
    String fileName = title.replace("/", "-") + "-girl.jpg";
    File file = new File(appDir, fileName);
    FileOutputStream outputStream;
    try {
        outputStream = new FileOutputStream(file);
        assert bitmap != null;
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return Uri.fromFile(file);
}

From source file:io.github.bunnyblue.droidfix.classcomputer.ClassComputer.java

public static void copyDiffClasses(ArrayList<ClassObject> diffClasses, String rootPath) {

    for (ClassObject classObject : diffClasses) {
        classObject.getClassName().replaceAll(".", "/");

        String subPath = classObject.getClassName().replaceAll("\\.", "/");
        if (subPath.lastIndexOf("/") != -1) {
            subPath = subPath.substring(0, subPath.lastIndexOf("/"));
            subPath = rootPath + "/" + subPath;
            subPath = subPath.replaceAll("\\\\", "/");
            File subDir = new File(subPath);
            subDir.mkdirs();
            File localClass = new File(classObject.getLocalPath());
            try {
                FileUtils.copyFileToDirectory(localClass, subDir);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();/*from w ww .  j a va  2 s.  c o  m*/
            }
            System.err.println("Copy diff class " + localClass.getAbsolutePath());

        }

    }
}