Java File Content Read getFileContent(File file)

Here you can find the source of getFileContent(File file)

Description

get File Content

License

Common Public License

Declaration

public static String getFileContent(File file) 

Method Source Code

//package com.java2s;
//Software released under Common Public License (CPL) v1.0

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

public class Main {
    public static String getFileContent(File file) {
        FileInputStream f = null;
        try {/*from  w  w w  .j a v a 2 s .  c o  m*/
            byte[] buffer = new byte[(int) file.length()];

            f = new FileInputStream(file);
            f.read(buffer);
            return new String(buffer);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            closeAll(f);
        }
    }

    public static void closeAll(Closeable... closeables) {
        if (closeables != null) {
            for (Closeable c : closeables) {
                try {
                    c.close();
                } catch (Exception e) {
                    // empty
                }
            }
        }
    }
}

Related

  1. getFileContent(File file)
  2. getFileContent(File file)
  3. getFileContent(File file)
  4. getFileContent(File file)
  5. getFileContent(File file)
  6. getFileContent(File file)
  7. getFileContent(File file)
  8. getFileContent(File file)
  9. getFileContent(File file)