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

public static StringBuilder readFile(String filePath, String charsetName) {
    File file = new File(filePath);
    StringBuilder fileContent = new StringBuilder("");
    if (!file.isFile()) {
        return null;
    }//w ww  .  j av  a 2 s  .  c  o m

    BufferedReader reader = null;
    try {
        InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName);
        reader = new BufferedReader(is);
        String line;
        while ((line = reader.readLine()) != null) {
            if (!fileContent.toString().equals("")) {
                fileContent.append("\r\n");
            }
            fileContent.append(line);
        }
        reader.close();
        return fileContent;
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
}

From source file:com.liferay.ide.core.util.FileListing.java

public static List<File> getFileListing(File dir) throws FileNotFoundException {
    List<File> result = new ArrayList<>();

    File[] files = dir.listFiles();

    if (ListUtil.isEmpty(files)) {
        return Collections.emptyList();
    }/* w  w  w  .j a v a2  s  .  com*/

    for (File file : files) {
        result.add(file);

        if (!file.isFile()) {
            List<File> deeperList = getFileListing(file);

            result.addAll(deeperList);
        }
    }

    return result;
}

From source file:Main.java

public static boolean DeleteFile(File file) {
    if (file == null || !file.exists()) {
        return false;
    }/*from   ww w .j a  v  a2 s  .co m*/
    if (file.isFile()) {
        return file.delete();
    }
    if (!file.isDirectory()) {
        return false;
    }
    File[] childFile = file.listFiles();
    if (childFile == null || childFile.length == 0) {
        return file.delete();
    }
    for (File f : childFile) {
        DeleteFile(f);
    }
    return file.delete();
}

From source file:de.oth.keycloak.initKeycloakServer.IT_TestAuth.java

@BeforeClass
public static void setUpClass() throws IOException {
    keycloak = initKeycloak(SERVER, REALM, USER, PWD, CLIENTSTR);
    String initFileStr = "integration_test_config.json";
    File initFile = new File(initFileStr);
    if (!initFile.isFile()) {
        URL url = TestRun.class.getClassLoader().getResource(initFileStr);
        if (url != null) {
            initFile = new File(url.getFile());
            if (!initFile.isFile()) {
                fail("test config is no file: " + initFileStr);
            }/*from   w w  w.j  a  va 2  s  . c  o  m*/
        } else {
            fail("can't load test config: " + initFileStr);
        }
    }

    RealmResource rRes = KeycloakAccess.getRealm(keycloak, GM_REALM, false);
    if (rRes != null) {
        rRes.remove();
    }
    rRes = KeycloakAccess.getRealm(keycloak, APP_REALM, false);
    if (rRes != null) {
        rRes.remove();
    }
    ObjectMapper mapper = new ObjectMapper();
    RealmsConfig realmsConfig = mapper.readValue(initFile, RealmsConfig.class);

    if (realmsConfig != null) {
        List<RealmConfig> realmList = realmsConfig.getRealms();
        if (realmList == null || realmList.isEmpty()) {
            fail("realmList is empty: " + initFileStr);
        }
        for (RealmConfig realmConf : realmList) {
            InitKeycloakServer.addRealm(keycloak, realmConf);
        }
    } else {
        fail("no realm config found in: " + initFileStr);
    }
}

From source file:Main.java

/**
 * Read file, one line as a element of the String List
 * //from  w ww  . j a v  a 2 s  .com
 * @param filePath
 *            The path of the file
 * @return List<String> Return file content as a String List, if the file
 *         doesn't exist return null
 */
public static List<String> readFileToList(String filePath) {
    File file = new File(filePath);
    List<String> fileContent = new ArrayList<String>();
    if (file != null && file.isFile()) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String line = null;
            while ((line = reader.readLine()) != null) {
                fileContent.add(line);
            }
            reader.close();
            return fileContent;
        } catch (IOException e) {
            throw new RuntimeException("IOException occurred. ", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    throw new RuntimeException("IOException occurred. ", e);
                }
            }
        }
    }
    return null;
}

From source file:io.fabric8.forge.rest.model.Models.java

/**
 * Saves the json object to the given file
 *//*www  .  j  a  v  a 2  s  .  c o m*/
public static <T> List<T> loadJsonValues(File json, Class<T> clazz) throws IOException {
    List<T> answer = new ArrayList<>();
    if (json.exists() && json.isFile()) {
        MappingIterator<T> iter = objectMapper.readerFor(clazz).readValues(json);
        while (iter.hasNext()) {
            answer.add(iter.next());
        }
    }
    return answer;
}

From source file:Main.java

public static void writeBytes(File dest, byte[] data, int off, int len, boolean append) throws IOException {
    if (dest.exists() == true) {
        if (dest.isFile() == false) {
            throw new IOException("Not a file: " + dest);
        }/*from w ww .j  a va 2  s .  c o  m*/
    }
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(dest, append);
        out.write(data, off, len);
    } finally {
        close(out);
    }
}

From source file:org.camunda.bpm.cockpit.test.util.DeploymentHelper.java

private static void addFiles(JavaArchive archive, String prefix, String rootPath, File dir) {

    for (File f : dir.listFiles()) {
        if (f.isFile()) {
            String filePath = f.getPath().replace(rootPath, "").replace("\\", "/");

            archive.addAsResource(f, prefix + filePath);
        } else {/*from  w  w  w .ja va2 s .  c o m*/
            addFiles(archive, prefix, rootPath, f);
        }
    }
}

From source file:Main.java

public static void copyfile(String fromFilePath, String toFilePath, Boolean rewrite) {
    File fromFile = new File(fromFilePath);
    File toFile = new File(toFilePath);

    if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) {
        return;/*ww w.  j  ava  2  s.  c om*/
    }

    if (!toFile.getParentFile().exists()) {
        toFile.getParentFile().mkdirs();
    }

    if (toFile.exists() && rewrite) {
        toFile.delete();
    }

    try {
        FileInputStream fosfrom = new FileInputStream(fromFile);
        FileOutputStream fosto = new FileOutputStream(toFile);

        byte[] bt = new byte[1024];
        int c;
        while ((c = fosfrom.read(bt)) > 0) {
            fosto.write(bt, 0, c);
        }
        fosfrom.close();
        fosto.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static boolean deleteFile(String path) {
    if (TextUtils.isEmpty(path)) {
        return true;
    }/*w w  w.java  2 s  . co  m*/

    File file = new File(path);
    if (!file.exists()) {
        return true;
    }
    if (file.isFile()) {
        return file.delete();
    }
    if (!file.isDirectory()) {
        return false;
    }
    for (File f : file.listFiles()) {
        if (f.isFile()) {
            f.delete();
        } else if (f.isDirectory()) {
            deleteFile(f.getAbsolutePath());
        }
    }
    return file.delete();
}