Java InputStreamReader Read readFile(String fileName)

Here you can find the source of readFile(String fileName)

Description

read File

License

Open Source License

Declaration

public static StringBuffer readFile(String fileName) throws FileNotFoundException 

Method Source Code

//package com.java2s;
/*************************************************************
 * This file is part of CB2XML.  /*from   w w w  .  j av a  2s .c  o m*/
 * See the file "LICENSE" for copyright information and the
 * terms and conditions for copying, distribution and
 * modification of CB2XML.
 *************************************************************
 */

import java.io.*;

public class Main {
    public static StringBuffer readFile(String fileName) throws FileNotFoundException {
        InputStream fis = openFile(fileName);
        BufferedReader buffer = null;
        StringBuffer sb = new StringBuffer();
        String s = null;
        try {
            buffer = new BufferedReader(new InputStreamReader(fis));
            while ((s = buffer.readLine()) != null) {
                sb.append(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    ;
                }
            }
        }
        return sb;
    }

    public static InputStream openFile(String fileName) throws FileNotFoundException {
        InputStream stream = ClassLoader.getSystemClassLoader().getResourceAsStream(fileName);
        if (stream == null)
            stream = new FileInputStream(fileName);
        if (stream == null)
            throw new FileNotFoundException("resources not found: " + fileName);
        return stream;
    }
}

Related

  1. readfile(String filename)
  2. readFile(String fileName)
  3. readFile(String fileName)
  4. readFile(String fileName)
  5. readFile(String fileName)
  6. readfile(String filename)
  7. readFile(String filename, boolean addEOL)
  8. readFile(String fileName, Class clazz)
  9. readFile(String fileName, String characterEncoding)