Example usage for java.io File isFile

List of usage examples for java.io File isFile

Introduction

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

Prototype

public boolean isFile() 

Source Link

Document

Tests whether the file denoted by this abstract pathname is a normal file.

Usage

From source file:com.recomdata.transmart.data.export.util.ZipUtil.java

/**
 * This method will zip a given folder.//from  w  w  w . jav  a 2 s. com
 * 
 * @param srcFolder
 * @param destZipFile
 * @throws Exception
 */
static public String zipFolder(String srcFolder, String destZipFile) throws Exception {
    File zipFile = null;
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;

    zipFile = new File(destZipFile);
    if (zipFile.exists() && zipFile.isFile() && zipFile.delete()) {
        zipFile = new File(destZipFile);
    }

    fileWriter = new FileOutputStream(zipFile);
    zip = new ZipOutputStream(fileWriter);

    addFolderToZip("", srcFolder, zip);
    zip.flush();
    zip.close();

    return zipFile.getName();
}

From source file:uk.ac.sanger.cgp.wwdocker.actions.Utils.java

public static File expandUserFile(BaseConfiguration config, String parameter, boolean checkExists) {
    File thing = expandUserFilePath(config, parameter, checkExists);
    if (!thing.isFile()) {
        throw new RuntimeException("Path indicated by '" + parameter + "=" + config.getString(parameter)
                + "' is not a file once expanded to '" + thing.getAbsolutePath() + "'");
    }//from  w  w w .  j a va2 s  . com
    return thing;
}

From source file:com.hazelcast.test.starter.HazelcastVersionLocator.java

private static File downloadFile(String url, File targetDirectory, String filename) {
    CloseableHttpClient client = HttpClients.createDefault();
    File targetFile = new File(targetDirectory, filename);
    if (targetFile.isFile() && targetFile.exists()) {
        return targetFile;
    }/*  w ww. j  av a2s.  c o  m*/
    HttpGet request = new HttpGet(url);
    try {
        CloseableHttpResponse response = client.execute(request);
        if (response.getStatusLine().getStatusCode() != SC_OK) {
            throw new GuardianException("Cannot download file from " + url + ", http response code: "
                    + response.getStatusLine().getStatusCode());
        }
        HttpEntity entity = response.getEntity();
        FileOutputStream fos = new FileOutputStream(targetFile);
        entity.writeTo(fos);
        fos.close();
        targetFile.deleteOnExit();
        return targetFile;
    } catch (IOException e) {
        throw rethrowGuardianException(e);
    } finally {
        try {
            client.close();
        } catch (IOException e) {
            // ignore
        }
    }
}

From source file:com.clustercontrol.collect.util.ZipCompresser.java

/**
 * ?????1????/*from  w  ww .ja v a  2  s .c o  m*/
 * 
 * @param inputFileNameList ??
 * @param outputFileName ?
 * 
 * @return ???
 */
protected static synchronized void archive(ArrayList<String> inputFileNameList, String outputFileName)
        throws HinemosUnknown {
    m_log.debug("archive() output file = " + outputFileName);

    byte[] buf = new byte[128];
    BufferedInputStream in = null;
    ZipEntry entry = null;
    ZipOutputStream out = null;

    // ?
    if (outputFileName == null || "".equals(outputFileName)) {
        HinemosUnknown e = new HinemosUnknown("archive output fileName is null ");
        m_log.info("archive() : " + e.getClass().getSimpleName() + ", " + e.getMessage());
        throw e;
    }
    File zipFile = new File(outputFileName);
    if (zipFile.exists()) {
        if (zipFile.isFile() && zipFile.canWrite()) {
            m_log.debug("archive() output file = " + outputFileName
                    + " is exists & file & writable. initialize file(delete)");
            if (!zipFile.delete())
                m_log.debug("Fail to delete " + zipFile.getAbsolutePath());
        } else {
            HinemosUnknown e = new HinemosUnknown("archive output fileName is directory or not writable ");
            m_log.info("archive() : " + e.getClass().getSimpleName() + ", " + e.getMessage());
            throw e;
        }
    }

    // ?
    try {
        // ????
        out = new ZipOutputStream(new FileOutputStream(outputFileName));

        for (String inputFileName : inputFileNameList) {
            m_log.debug("archive() input file name = " + inputFileName);

            // ????
            in = new BufferedInputStream(new FileInputStream(inputFileName));

            // ??
            String fileName = (new File(inputFileName)).getName();
            m_log.debug("archive() entry file name = " + fileName);
            entry = new ZipEntry(fileName);

            out.putNextEntry(entry);
            // ????
            int size;
            while ((size = in.read(buf, 0, buf.length)) != -1) {
                out.write(buf, 0, size);
            }
            // ??
            out.closeEntry();
            in.close();
        }
        // ? out.flush();
        out.close();

    } catch (IOException e) {
        m_log.warn("archive() archive error : " + outputFileName + e.getClass().getSimpleName() + ", "
                + e.getMessage(), e);
        throw new HinemosUnknown("archive error " + outputFileName, e);

    } finally {

        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }

        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:io.apicurio.hub.api.bitbucket.BitbucketSourceConnectorTest.java

@BeforeClass
public static void globalSetUp() {
    File credsFile = new File(".bitbucket");
    if (!credsFile.isFile()) {
        return;//from   w w  w.j  a v a2s.c om
    }
    System.out.println("Loading Bitbucket credentials from: " + credsFile.getAbsolutePath());
    try (Reader reader = new FileReader(credsFile)) {
        Properties props = new Properties();
        props.load(reader);
        String userPass = props.getProperty("username") + ":" + props.getProperty("password");
        basicAuth = Base64.encodeBase64String(userPass.getBytes(StandardCharsets.UTF_8));
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

From source file:com.prowidesoftware.swift.utils.Lib.java

/**
 * Read the content of the given file into a string.
 *
 * @param file the file to be read//from   w w w .  j a v a  2s  .  c  o  m
 * @param encoding encoding to use
 * @return the file contents or null if file is null or does not exist, or can't be read, or is not a file
 * @throws IOException if an error occurs during read
 */
public static String readFile(final File file, final String encoding) throws IOException {
    if (file == null || !file.exists() || !file.canRead() || !file.isFile()) {
        return null;
    }
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
    final StringBuilder sb = new StringBuilder((int) file.length());
    try {
        int c = 0;
        while ((c = in.read()) != -1) {
            sb.append((char) c);
        }
    } finally {
        in.close();
    }
    return sb.toString();
}

From source file:com.badlogic.gdx.box2deditor.utils.FileUtils.java

/**
 * Get the relative path from one file to another, specifying the directory separator.
 * If one of the provided resources does not exist, it is assumed to be a file unless it ends with '/' or
 * '\'.//from   www  . j ava2 s . com
 *
 * @param target targetPath is calculated to this file
 * @param base basePath is calculated from this file
 * @param separator directory separator. The platform default is not assumed so that we can test Unix behaviour when running on Windows (for example)
 * @return
 */
public static String getRelativePath(String targetPath, String basePath, String pathSeparator) {
    // Normalize the paths
    String normalizedTargetPath = FilenameUtils.normalizeNoEndSeparator(targetPath);
    String normalizedBasePath = FilenameUtils.normalizeNoEndSeparator(basePath);

    // Undo the changes to the separators made by normalization
    if (pathSeparator.equals("/")) {
        normalizedTargetPath = FilenameUtils.separatorsToUnix(normalizedTargetPath);
        normalizedBasePath = FilenameUtils.separatorsToUnix(normalizedBasePath);

    } else if (pathSeparator.equals("\\")) {
        normalizedTargetPath = FilenameUtils.separatorsToWindows(normalizedTargetPath);
        normalizedBasePath = FilenameUtils.separatorsToWindows(normalizedBasePath);

    } else {
        throw new IllegalArgumentException("Unrecognised dir separator '" + pathSeparator + "'");
    }

    String[] base = normalizedBasePath.split(Pattern.quote(pathSeparator));
    String[] target = normalizedTargetPath.split(Pattern.quote(pathSeparator));

    // First get all the common elements. Store them as a string,
    // and also count how many of them there are.
    StringBuilder common = new StringBuilder();

    int commonIndex = 0;
    while (commonIndex < target.length && commonIndex < base.length
            && target[commonIndex].equals(base[commonIndex])) {
        common.append(target[commonIndex]).append(pathSeparator);
        commonIndex++;
    }

    if (commonIndex == 0) {
        // No single common path element. This most
        // likely indicates differing drive letters, like C: and D:.
        // These paths cannot be relativized.
        throw new NoCommonPathFoundException("No common path element found for '" + normalizedTargetPath
                + "' and '" + normalizedBasePath + "'");
    }

    // The number of directories we have to backtrack depends on whether the base is a file or a dir
    // For example, the relative path from
    //
    // /foo/bar/baz/gg/ff to /foo/bar/baz
    //
    // ".." if ff is a file
    // "../.." if ff is a directory
    //
    // The following is a heuristic to figure out if the base refers to a file or dir. It's not perfect, because
    // the resource referred to by this path may not actually exist, but it's the best I can do
    boolean baseIsFile = true;

    File baseResource = new File(normalizedBasePath);

    if (baseResource.exists()) {
        baseIsFile = baseResource.isFile();

    } else if (basePath.endsWith(pathSeparator)) {
        baseIsFile = false;
    }

    StringBuilder relative = new StringBuilder();

    if (base.length != commonIndex) {
        int numDirsUp = baseIsFile ? base.length - commonIndex - 1 : base.length - commonIndex;

        for (int i = 0; i < numDirsUp; i++) {
            relative.append("..").append(pathSeparator);
        }
    }
    relative.append(normalizedTargetPath.substring(common.length()));
    return relative.toString();
}

From source file:de.hasait.genesis.base.util.GenesisUtils.java

public static String readFileToString(final File pFile) throws IOException {
    return pFile != null && pFile.isFile() ? FileUtils.readFileToString(pFile) : null;
}

From source file:com.itmanwuiso.checksums.FileDuplicateChecker.java

private static void createTaskes(String pathname) {

    File f = new File(pathname);

    if (f.isFile()) {
        createTaske(f);// ww  w.j  a  va2s  .  c  o  m
    } else if (f.isDirectory()) {

        File[] listfiles = f.listFiles();
        for (int i = 0; i < listfiles.length; i++) {
            if (recursive && listfiles[i].isDirectory()) {
                createTaskes(listfiles[i].getAbsolutePath());
            } else {
                createTaske(listfiles[i]);
            }
        }
    }

}

From source file:Main.java

public static List<File> listFile(File file) {
    List<File> list = new ArrayList<File>();
    List<File> dirList = new ArrayList<File>();
    if (file != null && file.exists()) {
        if (file.isFile()) {
            list.add(file);/* w  w w  .  j av  a2 s  .  co m*/
        } else if (file.isDirectory()) {
            if (!isNoMediaDir(file)) {
                File[] files = file.listFiles();
                if (files != null) {
                    for (File childFile : files) {
                        if (childFile.isFile()) {
                            list.add(childFile);
                        } else if (childFile.isDirectory()) {
                            dirList.add(childFile);
                        }
                    }
                    while (!dirList.isEmpty()) {
                        File dir = dirList.remove(0);
                        if (isNoMediaDir(dir)) {
                            continue;
                        }
                        File[] listFiles = dir.listFiles();
                        if (listFiles != null) {
                            for (File childFile : listFiles) {
                                if (childFile.isFile()) {
                                    list.add(childFile);
                                } else if (childFile.isDirectory()) {
                                    dirList.add(childFile);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return list;
}