Example usage for java.io File getAbsoluteFile

List of usage examples for java.io File getAbsoluteFile

Introduction

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

Prototype

public File getAbsoluteFile() 

Source Link

Document

Returns the absolute form of this abstract pathname.

Usage

From source file:Main.java

public static String browseForFile(Window owner, String file, int selectionMode, String title,
        FileFilter filter) {/*from   w  w  w . jav a  2 s . c  om*/
    final String curDir = System.getProperty("user.dir");
    JFileChooser chooser = new JFileChooser(curDir);
    chooser.setDialogType(JFileChooser.OPEN_DIALOG);
    chooser.setFileSelectionMode(selectionMode);
    chooser.setApproveButtonText("Select");
    chooser.setApproveButtonMnemonic('s');
    chooser.setDialogTitle(title);
    if (filter != null)
        chooser.setFileFilter(filter);

    if (file != null && !file.isEmpty()) {
        File curFile = new File(file);

        chooser.setCurrentDirectory(curFile.getAbsoluteFile().getParentFile());

        if (curFile.isDirectory()) {
            try {
                chooser.setSelectedFile(curFile.getCanonicalFile());
            } catch (IOException ex) {
            }
        } else {
            chooser.setSelectedFile(curFile);
        }
    }

    if (chooser.showOpenDialog(owner) == JFileChooser.APPROVE_OPTION) {
        String path = chooser.getSelectedFile().getPath();
        try {
            path = new File(path).getCanonicalPath();
        } catch (IOException e) {
        }
        // make path relative if possible
        if (path.startsWith(curDir)) {
            path = "." + path.substring(curDir.length());
        }

        return path;
    }
    return null;
}

From source file:Main.java

/**
 * Gets canonical file./*from w w w . j a v  a 2  s. c  o  m*/
 *
 * @param filePath
 *          String - file path
 * @return file File
 */
private static File getCanonicalFile(final String filePath) {
    final File file = new File(filePath);
    try {
        return file.getCanonicalFile();
    } catch (final IOException e) {
        return file.getAbsoluteFile();
    }
}

From source file:eu.freme.eservices.epublishing.Unzipper.java

public static void unzip(ZipInputStream zis, File outputFolder) throws IOException {
    //create output directory is not exists

    if (!outputFolder.exists() && !outputFolder.mkdirs()) {
        throw new IOException("Cannot create directory " + outputFolder);
    }/*from   w  ww .  ja  v a  2s  .c om*/

    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {
        String fileName = ze.getName();
        File newFile = new File(outputFolder, fileName);

        logger.debug("file unzip : " + newFile.getAbsoluteFile());

        //create all non exists folders
        //else you will hit FileNotFoundException for compressed folder
        File parentDir = newFile.getParentFile();
        if (!parentDir.exists() && !parentDir.mkdirs()) {
            throw new IOException("Cannot create directory " + newFile.getParent());
        }

        if (ze.isDirectory()) {
            newFile.mkdirs();
        } else {
            try (FileOutputStream fos = new FileOutputStream(newFile)) {
                IOUtils.copyLarge(zis, fos);
            }
        }
        ze = zis.getNextEntry();
    }
    zis.closeEntry();
}

From source file:Main.java

public static void unZip(String zipFile, String outputFolder) {
    byte[] buffer = new byte[BUFFER_SIZE];

    try {//w ww .j  av a2 s.  c o  m
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);

            System.out.println("file unzip : " + newFile.getAbsoluteFile());
            if (ze.isDirectory()) {
                newFile.mkdirs();
            } else {
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
            }
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
        System.out.println("Done");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.ibm.wala.cast.js.nodejs.NodejsRequiredSourceModule.java

/**
 * Generate a className based on the file name and path of the module file.
 * The path should be encoded in the className since the file name is not unique.
 * /*from  www. java 2  s . c o m*/
 * @param rootDir
 * @param file
 */
public static String convertFileToClassName(File rootDir, File file) {
    URI normalizedWorkingDirURI = rootDir.getAbsoluteFile().toURI().normalize();
    URI normalizedFileURI = file.getAbsoluteFile().toURI().normalize();
    String relativePath = normalizedWorkingDirURI.relativize(normalizedFileURI).getPath();

    return FilenameUtils.removeExtension(relativePath).replace("/", "_").replace("-", "__").replace(".", "__");
}

From source file:eu.edisonproject.classification.tfidf.mapreduce.TFIDFDriverImpl.java

public static void text2Avro(String inputPath, String outputPath) {
    Logger.getLogger(TFIDFDriverImpl.class.getName()).log(Level.INFO, "Start");
    try {/*from  w ww.jav a  2 s.  co  m*/
        File out = new File(outputPath);
        out.getAbsoluteFile().delete();
        out.getAbsoluteFile().mkdirs();

        IDataPrepare dp = new DataPrepare(inputPath, outputPath, STOPWORDS_PATH);
        dp.execute();
    } catch (Exception ex) {
        Logger.getLogger(TFIDFDriverImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Main.java

/**
 * Returns a reference to a file with the specified name that is located
 * somewhere on the classpath. The code for this method is an adaptation of
 * code supplied by Dave Postill.//from w  w  w .j av a  2  s .  c  o m
 * 
 * @param name
 *          the filename.
 * 
 * @return a reference to a file or <code>null if no file could be
 *         found.
 */
public static File findFileOnClassPath(final String name) {

    final String classpath = System.getProperty("java.class.path");
    final String pathSeparator = System.getProperty("path.separator");

    final StringTokenizer tokenizer = new StringTokenizer(classpath, pathSeparator);

    while (tokenizer.hasMoreTokens()) {
        final String pathElement = tokenizer.nextToken();

        final File directoryOrJar = new File(pathElement);
        final File absoluteDirectoryOrJar = directoryOrJar.getAbsoluteFile();

        if (absoluteDirectoryOrJar.isFile()) {
            final File target = new File(absoluteDirectoryOrJar.getParent(), name);
            if (target.exists()) {
                return target;
            }
        } else {
            final File target = new File(directoryOrJar, name);
            if (target.exists()) {
                return target;
            }
        }

    }
    return null;

}

From source file:com.fredhopper.core.connector.index.FileUtils.java

/**
 * Create a subdirectory of parentDir. The name is a random 15 character alphabetic string
 *
 * @param parentDir//  w w w .j  av a2  s  .co  m
 * @throws IOException
 */
public static File createRandomDirectory(final File parentDir) throws IOException {

    final String tempDirName = RandomStringUtils.randomAlphabetic(15);
    final File tempDir = new File(parentDir.getAbsoluteFile() + File.separator + tempDirName);
    org.apache.commons.io.FileUtils.forceMkdir(tempDir);
    return tempDir;

}

From source file:com.frostwire.util.ZipUtils.java

private static void unzipEntries(File folder, ZipInputStream zis, int itemCount, long time,
        ZipListener listener) throws IOException, FileNotFoundException {
    ZipEntry ze = null;//  w  ww.  j a  v a  2 s  .c  om

    int item = 0;

    while ((ze = zis.getNextEntry()) != null) {
        item++;

        String fileName = ze.getName();
        File newFile = new File(folder, fileName);

        LOG.debug("unzip: " + newFile.getAbsoluteFile());

        if (ze.isDirectory()) {
            if (!newFile.mkdirs()) {
                break;
            }
            continue;
        }

        if (listener != null) {
            int progress = (item == itemCount) ? 100 : (int) (((double) (item * 100)) / (double) (itemCount));
            listener.onUnzipping(fileName, progress);
        }

        FileOutputStream fos = new FileOutputStream(newFile);

        try {
            int n;
            byte[] buffer = new byte[1024];
            while ((n = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, n);

                if (listener != null && listener.isCanceled()) { // not the best way
                    throw new IOException("Uncompress operation cancelled");
                }
            }
        } finally {
            fos.close();
            zis.closeEntry();
        }

        newFile.setLastModified(time);
    }
}

From source file:aiai.apps.commons.utils.ZipUtils.java

/**
 * Creates a zip entry for the path specified with a name built from the base passed in and the file/directory
 * name. If the path is a directory, a recursive call is made such that the full directory is added to the zip.
 *
 * @param zOut The zip file's output stream
 * @param f The filesystem path of the file/directory being added
 * @param base The base prefix to for the name of the zip file entry
 *
 * @throws IOException If anything goes wrong
 *//*from   www  .j  av  a 2  s .c om*/
private static void addFileToZip(ZipArchiveOutputStream zOut, File f, String base) throws IOException {
    String entryName = base + f.getName();
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);

    zOut.putArchiveEntry(zipEntry);

    if (f.isFile()) {
        try (FileInputStream fInputStream = new FileInputStream(f)) {
            IOUtils.copy(fInputStream, zOut);
            zOut.closeArchiveEntry();
        }
    } else {
        zOut.closeArchiveEntry();
        File[] children = f.listFiles();

        if (children != null) {
            for (File child : children) {
                addFileToZip(zOut, child.getAbsoluteFile(), entryName + "/");
            }
        }
    }
}