Here you can find the source of readFromFile(String path)
Parameter | Description |
---|---|
path | a parameter |
public static String readFromFile(String path)
//package com.java2s; //License from project: Open Source License import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Main { /**/*from www . j ava2s.c o m*/ * * @param path * @return * * Since it is used in GWT context Java.nio can not be used and * guava comes in the wrong flavor * */ public static String readFromFile(String path) { File file = new File(path); if (!file.exists()) return ""; if (file.isDirectory()) throwRuntimeException("file expected, this is a directory: " + path); try { FileInputStream fin = new FileInputStream(file); return readFromStream(fin); } catch (IOException e) { throwRuntimeException(e.getMessage()); } return ""; } private static void throwRuntimeException(String msg) { throw new RuntimeException(msg); } public static String readFromStream(InputStream input) { StringBuilder sb = new StringBuilder(); BufferedInputStream bin = null; try { bin = new BufferedInputStream(input); byte[] contents = new byte[1024]; int bytesRead = 0; String strFileContents; while ((bytesRead = bin.read(contents)) != -1) { strFileContents = new String(contents, 0, bytesRead); sb.append(strFileContents); } } catch (IOException e) { throwRuntimeException(e.getMessage()); } finally { try { if (bin != null) bin.close(); } catch (IOException ioe) { } } return sb.toString(); } }