Example usage for java.io File getParentFile

List of usage examples for java.io File getParentFile

Introduction

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

Prototype

public File getParentFile() 

Source Link

Document

Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent directory.

Usage

From source file:org.jboss.as.test.integration.web.annotationsmodule.WebModuleDeploymentTestCase.java

@AfterClass
public static void tearDown() throws Exception {
    File testModuleRoot = new File(getModulePath(), "org/jboss/test/webModule");
    File file = testModuleRoot;
    while (!getModulePath().equals(file.getParentFile()))
        file = file.getParentFile();//from ww  w . j  a v  a 2 s .com
    deleteRecursively(file);
}

From source file:com.beginner.core.utils.FileZip.java

/**
 * ?ZIP?/*from   w w w . j av a 2s  .com*/
 * @param zip          ZIP
 * @param passwd       ZIP?
 * @return             ?
 * @throws ZipException ???
 */
public static File[] unzip(String zip, String passwd) throws ZipException {
    File zipFile = new File(zip);
    File parentDir = zipFile.getParentFile();
    return unzip(zipFile, parentDir.getAbsolutePath(), passwd);
}

From source file:org.pgptool.gui.config.impl.ConfigRepositoryImpl.java

public static void writeObject(Object o, String destinationFile) {
    ObjectOutputStream oos = null;
    try {//w  w  w.j  ava 2s  . c  o  m
        log.trace(String.format("Persisting %s to %s", o, destinationFile));

        File file = new File(destinationFile);
        if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) {
            throw new RuntimeException("Failed to create all parent directories");
        }

        FileOutputStream fout = new FileOutputStream(file);
        oos = new ObjectOutputStream(fout);
        oos.writeObject(o);
        oos.flush();
        oos.close();
        fout.close();
    } catch (Throwable t) {
        throw new RuntimeException("Failed to write config: " + destinationFile, t);
    } finally {
        safeClose(oos);
    }
}

From source file:Main.java

public static void copy(InputStream stream, String path) throws IOException {

    final File file = new File(path);
    if (file.exists()) {
        file.delete();/*from   w  w w  .  java 2  s. c o m*/
    }
    final File parentFile = file.getParentFile();

    if (!parentFile.exists()) {
        //noinspection ResultOfMethodCallIgnored
        parentFile.mkdir();
    }

    if (file.exists()) {
        return;
    }
    OutputStream myOutput = new FileOutputStream(path, true);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = stream.read(buffer)) >= 0) {
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    stream.close();

}

From source file:com.chingo247.structureapi.plan.PlanGenerator.java

private static void generatePlanFromSchematic(File file, File rootDirectory) throws IOException {
    String name = FilenameUtils.getBaseName(file.getName());
    File directory = file.getParentFile();

    Document d = DocumentHelper.createDocument();
    Element root = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_ROOT_ELEMENT);
    d.add(root);//from   w ww  .  j ava  2s . com

    Element nameElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_NAME_ELEMENT);
    nameElement.setText(name);
    root.add(nameElement);

    Element priceElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_PRICE_ELEMENT);
    priceElement.setText("0");
    root.add(priceElement);

    Element description = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_DESCRIPTION_ELEMENT);
    description.setText("None");
    root.add(description);

    Element categoryElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_CATEGORY_ELEMENT);
    String category = rootDirectory.getName().equals(directory.getName()) ? "Default" : directory.getName();
    categoryElement.setText(category);
    root.add(categoryElement);

    Element placementElment = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_PLACEMENT);

    Element typeElement = new BaseElement(PlacementXMLConstants.TYPE_ELEMENT);
    typeElement.setText(PlacementTypes.SCHEMATIC);

    Element schematicElement = new BaseElement(PlacementXMLConstants.SCHEMATIC_ELEMENT);
    schematicElement.setText(file.getName());

    placementElment.add(typeElement);
    placementElment.add(schematicElement);

    root.add(placementElment);

    File planFile = new File(directory, name + ".xml");
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(new FileWriter(planFile), format);
    writer.write(d);
    writer.close();
}

From source file:com.isomorphic.maven.util.ArchiveUtils.java

/**
 * Builds a ZIP file from the contents of a directory on the filesystem (recursively).
 * /*from ww  w .  ja v a  2  s  . c  om*/
 * @see http://stackoverflow.com/questions/1281229/how-to-use-jaroutputstream-to-create-a-jar-file
 * 
 * @param directory the directory containing the content to be xzipped up
 * @param output the zip file to be written to
 * @throws IOException
 */
public static void zip(File directory, File output) throws IOException {
    output.getParentFile().mkdirs();
    ZipOutputStream target = new ZipOutputStream(new FileOutputStream(output));
    zip(directory, directory, target);
    target.close();
}

From source file:net.orpiske.ssps.common.repository.RepositorySettings.java

/**
 * Initializes the configuration object//from  w  w w  .j a  v  a 2 s.c om
 * 
 * @throws SspsException if the configuration file is not found, if the configuration
 * file is not parseable or if unable to create directories
 */
public static void initConfiguration() throws SspsException {

    String repositoryPath = RepositoryUtils.getUserRepository();
    String path = repositoryPath + File.separator + "repositories.conf";

    if (config == null) {

        File file = new File(path);

        if (!file.exists()) {

            try {
                if (!file.getParentFile().mkdirs()) {
                    throw new SspsException("Unable to create parent directories");
                }

                if (!file.createNewFile()) {
                    throw new SspsException("Unable to create repository settings file");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                config = new PropertiesConfiguration(path);
            } catch (ConfigurationException e) {
                throw new SspsException("Unable to load repository configuration: " + e.getMessage(), e);
            }

            try {
                config.save();
            } catch (ConfigurationException e) {
                throw new SspsException("Unable to save repository configuration: " + e.getMessage(), e);
            }
        } else {
            try {
                config = new PropertiesConfiguration(path);
            } catch (ConfigurationException e) {
                throw new SspsException("Unable to load repository configuration: " + e.getMessage(), e);
            }
        }

    }
}

From source file:fr.asso.vieillescharrues.parseurs.TelechargeFichier.java

/**
 * Mthode statique grant le tlechargement de fichiers
 * @param url Adresse du fichier/*from  w w w .ja v  a2 s. c om*/
 * @param fichierDest Nom du ficher en local
 */
public static void DownloadFromUrl(URL url, String fichierDest) throws IOException {
    File file;
    if (fichierDest.endsWith(".jpg"))
        file = new File(PATH + "images/", fichierDest);
    else
        file = new File(PATH, fichierDest);
    file.getParentFile().mkdirs();
    URLConnection ucon = url.openConnection();

    try {
        tailleDistant = ucon.getHeaderFieldInt("Content-Length", 0); //Rcupre le header HTTP Content-Length
        tailleLocal = (int) file.length();
    } catch (Exception e) {
        e.printStackTrace();
    }
    // Compare les tailles des fichiers
    if ((tailleDistant == tailleLocal) && (tailleLocal != 0))
        return;

    InputStream is = ucon.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);
    ByteArrayBuffer baf = new ByteArrayBuffer(50);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(baf.toByteArray());
    fos.close();
}

From source file:de.flapdoodle.embed.process.io.file.Files.java

public static File createTempFile(File tempDir, String tempFileName)
        throws IOException, FileAlreadyExistsException {
    File tempFile = new File(tempDir, tempFileName);

    File parent = tempFile.getParentFile();
    if (!parent.exists() && !parent.mkdirs()) {
        throw new IllegalStateException("Couldn't create dir: " + parent);
    }//from  www  .java 2s  .  c  o m

    if (!tempFile.createNewFile())
        throw new FileAlreadyExistsException("Could not create Tempfile", tempFile);
    return tempFile;
}

From source file:Main.java

public static void compressAFileToZip(File zip, File source) throws IOException {
    Log.d("source: " + source.getAbsolutePath(), ", length: " + source.length());
    Log.d("zip:", zip.getAbsolutePath());
    zip.getParentFile().mkdirs();
    File tempFile = new File(zip.getAbsolutePath() + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ZipOutputStream zos = new ZipOutputStream(bos);
    ZipEntry zipEntry = new ZipEntry(source.getName());
    zipEntry.setTime(source.lastModified());
    zos.putNextEntry(zipEntry);/*w  w w  . j a v a 2  s. c o m*/

    FileInputStream fis = new FileInputStream(source);
    BufferedInputStream bis = new BufferedInputStream(fis);
    byte[] bArr = new byte[4096];
    int byteRead = 0;
    while ((byteRead = bis.read(bArr)) != -1) {
        zos.write(bArr, 0, byteRead);
    }
    zos.flush();
    zos.closeEntry();
    zos.close();
    Log.d("zipEntry: " + zipEntry, "compressedSize: " + zipEntry.getCompressedSize());
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    bis.close();
    fis.close();
    zip.delete();
    tempFile.renameTo(zip);
}