Check to see if a file exists

private static String readFileAsString(String filePath) throws java.io.IOException {
        File fp = new File(filePath);
        if (fp.exists()) {
                StringBuffer fileData = new StringBuffer(1000);
                BufferedReader reader = new BufferedReader(new FileReader(filePath));
                char[] buf = new char[1024];
                int numRead = 0;
                while ((numRead = reader.read(buf)) != -1) {
                        String readData = String.valueOf(buf, 0, numRead);
                        fileData.append(readData);
                        buf = new char[1024];
                }
                reader.close();
                return fileData.toString();
        } else {
                return "File does not exist";
        }
}

Page Comments (Click to edit)






[Click to add or edit comments])

Please prepend comments below including a date

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Have a nice day.