Java Text File Read All readAllText(final String filename)

Here you can find the source of readAllText(final String filename)

Description

read All Text

License

Apache License

Declaration

public static String readAllText(final String filename) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static String readAllText(final String filename) throws IOException {
        FileInputStream fis = null;
        try {/*from   w w w .j a v a 2  s.  c  om*/
            File file = new File(filename.toString());
            fis = new FileInputStream(file);
            int size = fis.available();
            byte[] contents = new byte[size];
            fis.read(contents);
            return new String(contents);
        } catch (Exception e) {
            throw new IOException(e);
        } finally {
            // in the absence of commons io, this workaround is needed to close the file
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
}

Related

  1. readAll(File file)
  2. readAll(File file)
  3. readAll(final String path)
  4. readAllFile(File file)
  5. readAllFile(String fileName)