Here you can find the source of fileAsString(String filePath)
Parameter | Description |
---|---|
filePath | A string containing a platform-acceptable file path. |
public static String fileAsString(String filePath)
//package com.java2s; import java.io.File; import java.io.FileInputStream; public class Main { /**/*from w w w .j a v a 2s.c o m*/ Read a file, putting its contents into a String. @param filePath A string containing a platform-acceptable file path. @return The file contents, converted to a String, using the default locale. Returns null if there are any problems. */ public static String fileAsString(String filePath) { String retVal = null; if (filePath == null) { return null; } try { FileInputStream fin = new FileInputStream(filePath); return fileAsString(fin); } catch (Exception exc) { // just go ahead and return the null } return retVal; } /** Read a file, putting its contents into a String. @param dir Directory where filepath is rooted @param filepath The file we want to read @return The file contents, converted to a String, using the default locale. Returns null if there are any problems. */ public static String fileAsString(File dir, String filepath) { File theF = new File(dir, filepath); return fileAsString(theF); } /** Read a file, putting its contents into a String. @param infile The file we want to read @return The file contents, converted to a String, using the default locale. Returns null if there are any problems. */ public static String fileAsString(File infile) { String retVal = null; try { FileInputStream fin = new FileInputStream(infile); return fileAsString(fin); } catch (Exception exc) { // just go ahead and return the null } return retVal; } /** Read a file, putting its contents into a String. @param fin An input stream. This method closes the stream @return The file contents, converted to a String, using the default locale. Returns null if there are any problems. */ public static String fileAsString(FileInputStream fin) { String retVal = null; try { int avail = fin.available(); byte[] bb = new byte[avail]; fin.read(bb); fin.close(); retVal = new String(bb); } catch (Exception exc) { // just go ahead and return the null } return retVal; } }