All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.shopizer.search.utils.FileUtil Maven / Gradle / Ivy

There is a newer version: 2.11.2
Show newest version
package com.shopizer.search.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class FileUtil {


  public static String readFileAsString(String fileName) throws java.io.IOException {


    InputStream is = null;
    Reader r = null;

    try {


      is = FileUtil.class.getClassLoader().getResourceAsStream(fileName);

      if (is == null) {
        throw new java.io.IOException("File " + fileName + " not found");
      }

      InputStreamReader in = new InputStreamReader(is);


      StringBuffer buffer = new StringBuffer();
      r = new BufferedReader(in);
      int ch;
      while ((ch = r.read()) > -1) {
        buffer.append((char) ch);
      }
      r.close();
      return buffer.toString();

    } finally {
      if (r != null) {
        try {

        } catch (Exception ignore) {
        }
      }
      if (is != null) {
        try {

        } catch (Exception ignore) {
        }
      }
    }


    // URL url = FileUtil.class.getResource(fileName);
    // String fullyQualifiedFilename = url.getFile();

    /*
     * BufferedReader reader = new BufferedReader( new FileReader(fileName));
     * 
     * char[] buf = new char[1024]; int numRead=0; while((numRead=reader.read(buf)) != -1){
     * fileData.append(buf, 0, numRead); } reader.close(); return fileData.toString();
     */
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy