×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
1
Language: Java
Posted by: Cuong Vo
Added: May 10, 2013 4:33 AM
Modified: May 20, 2013 2:34 AM
Views: 24
Tags: java
  1.   /** @param filePath the name of the file to open. Not sure if it can accept URLs or just filenames. Path handling could be better, and buffer sizes are hardcoded
  2.     */
  3.     private static String readFileAsString(String filePath)
  4.     throws java.io.IOException{
  5.         StringBuffer fileData = new StringBuffer(1000);
  6.         BufferedReader reader = new BufferedReader(
  7.                 new FileReader(filePath));
  8.         char[] buf = new char[1024];
  9.         int numRead=0;
  10.         while((numRead=reader.read(buf)) != -1){
  11.             String readData = String.valueOf(buf, 0, numRead);
  12.             fileData.append(readData);
  13.             buf = new char[1024];
  14.         }
  15.         reader.close();
  16.         return fileData.toString();
  17.     }