Example usage for java.io File getParent

List of usage examples for java.io File getParent

Introduction

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

Prototype

public String getParent() 

Source Link

Document

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

Usage

From source file:com.github.jankrause.javadoctools.exporters.DefinitionsAsHtmlExporter.java

public void export() throws IOException {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
    File tmpFile = File.createTempFile("tmp_", ".tpl");

    FileOutputStream copyFileOutputStream = null;
    InputStream templateInputStream = null;

    cfg.setDirectoryForTemplateLoading(new File(tmpFile.getParent()));
    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

    try {/*w  w w.  ja v a 2 s  . c  om*/
        templateInputStream = DefinitionsAsHtmlExporter.class.getResourceAsStream("glossary.tpl");
        copyFileOutputStream = new FileOutputStream(tmpFile);
        IOUtils.copy(templateInputStream, copyFileOutputStream);
    } finally {
        IOUtils.closeQuietly(templateInputStream);
        IOUtils.closeQuietly(copyFileOutputStream);
    }

    Writer out = null;

    try {
        Map<Character, List<Definition>> sortedByFirstCharacter = DefinitionUtils
                .sortByFirstCharacter(definitions);
        Template temp = cfg.getTemplate(tmpFile.getName());

        out = new FileWriter(this.outputFile);

        temp.process(sortedByFirstCharacter, out);
    } catch (TemplateException templateEx) {
        throw new IOException(templateEx);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:com.headwire.aem.tooling.intellij.eclipse.stub.IFile.java

public void create(InputStream content, boolean force, IProgressMonitor monitor) throws CoreException {
    File newFile = file;
    if (virtualFile != null && !virtualFile.exists()) {
        newFile = new File(virtualFile.getPath());
    }//from w  w  w .j a  v  a  2 s. co  m
    if (!newFile.exists()) {
        String parentPath = newFile.getParent();
        VirtualFile parent = module.getProject().getProjectFile().getFileSystem().findFileByPath(parentPath);
        if (parent != null) {
            VirtualFile child = null;
            try {
                child = parent.createChildData(this, newFile.getName());
                this.virtualFile = child;
                setContents(content, 0, null);
            } catch (IOException e) {
                throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        "Failed to create file: " + virtualFile, e));
            }
        } else {
            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    "Parent: " + parent + " could not be found"));
        }
    }
}

From source file:msearch.filmlisten.MSFilmlisteSchreiben.java

private void xmlSchreibenStart(String datei) throws IOException, XMLStreamException {
    File file = new File(datei);
    File dir = new File(file.getParent());
    if (!dir.exists()) {
        if (!dir.mkdirs()) {
            MSLog.fehlerMeldung(947623049, MSLog.FEHLER_ART_PROG,
                    "MSearchIoXmlFilmlisteSchreiben.xmlSchreibenStart",
                    "Kann den Pfad nicht anlegen: " + dir.toString());
        }/* ww  w  .  j  a va 2  s.  c  o  m*/
    }
    MSLog.systemMeldung("   --> Start Schreiben nach: " + datei);
    XMLOutputFactory outFactory = XMLOutputFactory.newInstance();
    if (datei.endsWith(MSConst.FORMAT_BZ2)) {
        bZip2CompressorOutputStream = new BZip2CompressorOutputStream(new FileOutputStream(file),
                9 /*Blocksize: 1 - 9*/);
        out = new OutputStreamWriter(bZip2CompressorOutputStream, MSConst.KODIERUNG_UTF);
    } else if (datei.endsWith(MSConst.FORMAT_ZIP)) {
        zipOutputStream = new ZipOutputStream(new FileOutputStream(file));
        ZipEntry entry = new ZipEntry(MSConst.XML_DATEI_FILME);
        zipOutputStream.putNextEntry(entry);
        out = new OutputStreamWriter(zipOutputStream, MSConst.KODIERUNG_UTF);
    } else {
        out = new OutputStreamWriter(new FileOutputStream(file), MSConst.KODIERUNG_UTF);
    }
    writer = outFactory.createXMLStreamWriter(out);
    writer.writeStartDocument("UTF-8", "1.0");
    writer.writeCharacters("\n");//neue Zeile
    writer.writeStartElement(MSConst.XML_START);
    writer.writeCharacters("\n");//neue Zeile
}

From source file:eu.optimis.ics.core.io.BaseImageCopier.java

/**
 * Creates a new file copier for creating <code>source</code> to
 * <code>destination</code> and storing the relevant information in
 * <code>image</code>.// ww  w.  j a v  a  2 s .  c o m
 * 
 * @param source
 *            the base image
 * @param destination
 *            the destination image file
 * @param image
 *            the {@link Image} object storing the relevant image
 *            information
 * @throws OutOfDiskSpaceException
 *             If there is not enough free space on the drive where
 *             <code>destination</code> resides
 */
public BaseImageCopier(File source, File destination, Image image) throws OutOfDiskSpaceException {
    this.source = source;
    this.destination = destination;
    this.image = image;

    if (checkDiskspace(destination.getParent(), source) == false) {
        throw new OutOfDiskSpaceException(
                "ics.core.io.BaseImageCopier: Not enough free space at target '" + destination + "'");
    }
}

From source file:com.owncloud.android.operations.RenameFileOperation.java

private void saveLocalFile() {
    mFile.setFileName(mNewName);//from  w  w  w .jav  a  2 s  .c  o m

    // try to rename the local copy of the file
    if (mFile.isDown()) {
        File f = new File(mFile.getStoragePath());
        String parentStoragePath = f.getParent();
        if (!parentStoragePath.endsWith(File.separator))
            parentStoragePath += File.separator;
        if (f.renameTo(new File(parentStoragePath + mNewName))) {
            mFile.setStoragePath(parentStoragePath + mNewName);
        }
        // else - NOTHING: the link to the local file is kept although the local name can't be updated
        // TODO - study conditions when this could be a problem
    }

    mStorageManager.saveFile(mFile);
}

From source file:ch.puzzle.itc.mobiliar.business.generator.control.GeneratorFileWriter.java

public void generateTemporaryFile(String fileName, String content) throws IOException, FileNotFoundException {

    File temp = new File(fileName);

    File directory = new File(temp.getParent());
    if (!directory.exists()) {
        directory.mkdirs();/* www . j av a2  s. c  o  m*/
    }

    if (temp.isFile() && temp.exists()) {
        String message = "The file already exist: " + temp.getName();
        log.log(Level.WARNING, message);
    } else {
        if (temp.createNewFile()) {
            writeFile(temp, content);
            log.info("File created: " + temp.getName());
        } else {
            String message = "The file was not created: " + temp.getName();
            log.log(Level.WARNING, message);
        }
    }

}

From source file:com.feilong.commons.core.io.FileUtilTest.java

/**
 * Test get p.//from w w w  .j a va 2  s  .c om
 */
@Test
// @Ignore
public void testGetP() {
    File file = new File(fString);
    log.info(file.getAbsolutePath());
    log.info(file.getParent());
}

From source file:com.android.tradefed.result.CodeCoverageReporter.java

/**
 * Tries to find emma.jar in same location as ddmlib.jar.
 *
 * @return full path to emma jar file//from w  ww.j a  v a  2 s .co  m
 * @throws AssertionError if could not find emma jar
 */
String findEmmaJarPath() {
    String ddmlibPath = IDevice.class.getProtectionDomain().getCodeSource().getLocation().getFile();
    Assert.assertFalse("failed to find ddmlib path", ddmlibPath.isEmpty());
    File parentFolder = new File(ddmlibPath);
    File emmaJar = new File(parentFolder.getParent(), "emmalib.jar");
    Assert.assertTrue(String.format("Failed to find emma.jar in %s", emmaJar.getAbsolutePath()),
            emmaJar.exists());
    CLog.d("Found emma jar at %s", emmaJar.getAbsolutePath());
    return emmaJar.getAbsolutePath();
}

From source file:com.iksgmbh.sql.pojomemodb.utils.FileUtil.java

/**
 * Uses streams to perform copy/*w  w w.j  a  va2s  . c  o  m*/
 * @param fromFile
 * @param toFile
 * @throws IOException
 */
public static void copyBinaryFile(final File fromFile, File toFile) {
    if (!fromFile.exists())
        throw new RuntimeException("FileCopy: " + "no such source file: " + fromFile.getAbsolutePath());
    if (!fromFile.isFile())
        throw new RuntimeException("FileCopy: " + "can't copy directory: " + fromFile.getAbsolutePath());
    if (!fromFile.canRead())
        throw new RuntimeException("FileCopy: " + "source file is unreadable: " + fromFile.getAbsolutePath());

    if (toFile.isDirectory())
        toFile = new File(toFile, fromFile.getName());

    if (toFile.exists()) {
        if (!toFile.canWrite())
            throw new RuntimeException(
                    "FileCopy: " + "destination file is unwriteable: " + toFile.getAbsolutePath());
    } else {
        String parent = toFile.getParent();
        if (parent == null)
            parent = System.getProperty("user.dir");
        File dir = new File(parent);
        if (!dir.exists())
            throw new RuntimeException("FileCopy: " + "destination directory doesn't exist: " + parent);
        if (dir.isFile())
            throw new RuntimeException("FileCopy: " + "destination is not a directory: " + parent);
        if (!dir.canWrite())
            throw new RuntimeException("FileCopy: " + "destination directory is unwriteable: " + parent);
    }

    FileInputStream from = null;
    FileOutputStream to = null;
    try {
        from = new FileInputStream(fromFile);
        to = new FileOutputStream(toFile);
        byte[] buffer = new byte[4096];
        int bytesRead;

        while ((bytesRead = from.read(buffer)) != -1)
            to.write(buffer, 0, bytesRead); // write
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (from != null)
            try {
                from.close();
            } catch (IOException e) {
                ;
            }
        if (to != null)
            try {
                to.close();
            } catch (IOException e) {
                ;
            }
    }
}

From source file:desktopsearch.WatchDir.java

private void UpdateDB(String Event, Path FullLocation) throws SQLException, IOException {

    File file = new File(FullLocation.toString());
    String path = file.getParent();
    String Name = file.getName();

    if (Name.contains("'")) {
        Name = Name.replace("'", "''");
    }//w ww . j a  va2  s. c  o  m
    if (path.contains("'")) {
        path = path.replace("'", "''");
    }

    if (Event.equals("ENTRY_CREATE") && file.exists()) {
        FileTime LastModifiedTime = Files.getLastModifiedTime(FullLocation);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(LastModifiedTime.toMillis());
        String Query;
        if (file.isFile()) {
            String Type = FilenameUtils.getExtension(FullLocation.toString());
            if (Type.endsWith("~") || Type.equals("")) {
                Type = "Text";
            }
            Query = "INSERT INTO FileInfo VALUES('" + path + "','" + Name + "','" + Type + "','"
                    + calendar.getTime() + "'," + (file.length() / 1024) + ");";
        } else if (!Files.isSymbolicLink(FullLocation)) {
            long size = FileUtils.sizeOfDirectory(file);
            Query = "INSERT INTO FileInfo VALUES('" + path + "','" + Name + "','FOLDER','" + calendar.getTime()
                    + "'," + size + ");";
        } else {
            Query = "INSERT INTO FileInfo VALUES('" + path + "','" + Name + "','LINK','" + calendar.getTime()
                    + "',0);";
        }
        statement.executeUpdate(Query);

    } else if (Event.equals("ENTRY_MODIFY")) {
        System.out.println(path + "/" + Name);
        FileTime LastModifiedTime = Files.getLastModifiedTime(FullLocation);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(LastModifiedTime.toMillis());
        String Query = "UPDATE FileInfo SET LastModified='" + calendar.getTime() + "' WHERE FileLocation='"
                + path + "' and FileName='" + Name + "';";
        System.out.println(Query);
        statement.executeUpdate(Query);

    } else if (Event.equals("ENTRY_DELETE")) {

        String Query = "DELETE FROM FileInfo WHERE FileLocation = '" + path + "' AND FileName = '" + Name
                + "';";
        statement.executeUpdate(Query);

    }
    connection.commit();

}