Java InputStreamReader Read readFile(String filename1)

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

Description

read File

License

Open Source License

Declaration

public static String readFile(String filename1) throws IOException 

Method Source Code


//package com.java2s;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static String readFile(String filename1) throws IOException {

        File f = new File(filename1);
        if (!f.exists())
            return null;

        FileInputStream fis = null;
        InputStreamReader fsr = null;

        StringBuilder out = new StringBuilder();

        int len;/* w w w  .ja  v a 2 s.  c  om*/
        char[] buffer = new char[1024];

        try {
            fis = new FileInputStream(filename1);
            fsr = new InputStreamReader(fis, "UTF-8");

            while (true) {
                len = fsr.read(buffer);
                if (len == -1)
                    break;
                out.append(buffer, 0, len);
            }
        } finally {

            try {
                if (fsr != null)
                    fsr.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                if (fis != null)
                    fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return out.toString();
    }
}

Related

  1. readFile(String fileName, String characterEncoding)
  2. readFile(String fileName, String characterEncoding)
  3. readFile(String fileName, String characterEncoding)
  4. readFile(String fileName, String charSet)
  5. readFile(String fileName, String coloredLineIndicator, boolean useSections, boolean isXML)
  6. readFile(String fileNm)
  7. readFile(String filePath)
  8. readFile(String filePath)
  9. readFile(String filePath)