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:Main.java

public static void writeToFile(Iterator<?> iter, File file) throws IOException {
    if (iter != null && file != null) {
        file.getParentFile().mkdirs();
        File tempFile = new File(file.getAbsolutePath() + ".tmp");
        FileWriter fw = new FileWriter(tempFile);
        for (; iter.hasNext();) {
            fw.write(new StringBuilder(iter.next().toString()).append("\r\n").toString());
        }//from  w ww.j a  v a2s  .c om
        fw.flush();
        fw.close();
        file.delete();
        tempFile.renameTo(file);
    }
}

From source file:edu.harvard.mcz.imagecapture.ThumbnailBuilder.java

public static File getThumbFileForFile(File file) {
    File thumbsDir = new File(file.getParentFile().getPath() + File.separator + "thumbs");
    File result = new File(thumbsDir.getPath().concat(File.separator).concat(file.getName()));
    return result;
}

From source file:net.sf.jabref.exporter.AutoSaveManager.java

/**
 * Get a File object pointing to the autosave file corresponding to the given file.
 * @param f The database file./*  w ww .  java2s  . co  m*/
 * @return its corresponding autosave file.
 */
public static File getAutoSaveFile(File f) {
    return new File(f.getParentFile(), ".$" + f.getName() + '$');
}

From source file:com.ALC.SC2BOAserver.util.SC2BOAserverFileUtil.java

/**
 * This method extracts data to a given directory.
 * /* www .j a  va 2 s . co  m*/
 * @param directory the directory to extract into
 * @param zipIn input stream pointing to the zip file
 * @throws ArchiveException
 * @throws IOException
 * @throws FileNotFoundException
 */

public static void extractZipToDirectory(File directory, InputStream zipIn)
        throws ArchiveException, IOException, FileNotFoundException {

    ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("zip", zipIn);
    while (true) {
        ZipArchiveEntry entry = (ZipArchiveEntry) in.getNextEntry();
        if (entry == null) {
            in.close();
            break;
        }
        //Skip empty files
        if (entry.getName().equals("")) {
            continue;
        }

        if (entry.isDirectory()) {
            File file = new File(directory, entry.getName());
            file.mkdirs();
        } else {
            File outFile = new File(directory, entry.getName());
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(outFile);
            IOUtils.copy(in, out);
            out.flush();
            out.close();
        }
    }
}

From source file:TestRdfToJsonConversion.java

private static FileInputStream makeFile(String fnameJson) throws IOException {
    File f = new File(fnameJson);
    f.getParentFile().mkdirs();
    f.createNewFile();//from  w  ww .j a  v  a2  s . c  o  m
    return new FileInputStream(f);
}

From source file:Main.java

public static void unzip(InputStream fin, String targetPath, Context context) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fin));
    try {//from  ww w.  j a v a  2s.c  om
        ZipEntry zipEntry;
        int count;
        byte[] buffer = new byte[8192];
        while ((zipEntry = zis.getNextEntry()) != null) {
            File file = new File(targetPath, zipEntry.getName());
            File dir = zipEntry.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs()) {
                throw new FileNotFoundException("Failed to get directory: " + dir.getAbsolutePath());
            }
            if (zipEntry.isDirectory()) {
                continue;
            }
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
            Log.d("TEST", "Unzipped " + file.getAbsolutePath());
        }
    } finally {
        zis.close();
    }
}

From source file:Main.java

public static void writeToFile(Collection<?> collection, File file) throws IOException {
    if (collection != null && file != null) {
        file.getParentFile().mkdirs();
        File tempFile = new File(file.getAbsolutePath() + ".tmp");
        FileWriter fw = new FileWriter(tempFile);
        for (Object obj : collection) {
            fw.write(new StringBuilder(obj.toString()).append("\r\n").toString());
        }// w  w  w .j a  v a 2s . com
        fw.flush();
        fw.close();
        file.delete();
        tempFile.renameTo(file);
    }
}

From source file:Main.java

public static boolean storeImage(Context context, Bitmap bmp, boolean isRotate) {

    // use the current data&time for image file name
    String takenTime_YYMMDD_HHMMSS = new SimpleDateFormat(DATA_FORMAT).format(new Date());

    // saved bitmap: full path
    String path = PIC_ROOT_PATH + takenTime_YYMMDD_HHMMSS;
    File f = new File(path);

    if (f != null && !f.getParentFile().exists()) {
        f.getParentFile().mkdirs();// www. j av a  2s  .  c o  m
    }

    if (isRotate) {
        Matrix matrix = new Matrix();
        matrix.reset();
        matrix.postRotate(90);
        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    }

    try {
        FileOutputStream out = new FileOutputStream(f);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    return updateGallery(context, bmp, takenTime_YYMMDD_HHMMSS);
}

From source file:com.incapture.rapgen.output.OutputWriter.java

/**
 * Write templates to files. If you have files composed of multiple templates, look at {@link #writeMultiPartTemplates(String, Map)}
 *
 * @param rootFolder/*  w  ww .ja  v  a  2  s  . c om*/
 * @param pathToTemplate
 */
public static void writeTemplates(String rootFolder, Map<String, StringTemplate> pathToTemplate) {
    for (Map.Entry<String, StringTemplate> entry : pathToTemplate.entrySet()) {
        String filename = entry.getKey();

        File file = new File(rootFolder, filename);
        file.getParentFile().mkdirs();

        StringTemplate template = entry.getValue();
        try {
            FileUtils.write(file, template.toString());
        } catch (IOException e) {
            System.err.println("Error writing template: " + ExceptionToString.format(e));
        }

    }
}

From source file:Main.java

private static File toFile(final File rootDir, final String path) {
    File file = rootDir;
    for (final String pathComponent : path.split("\\.")) {
        file = new File(file, pathComponent);
    }/*from w  w w. j a va  2  s .c om*/
    file = new File(file.getParentFile(), file.getName() + ".xml");
    return file;
}