Java FileInputStream Read readFile(String filePath)

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

Description

read File

License

Open Source License

Declaration

public static synchronized String readFile(String filePath) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.io.IOException;

public class Main {
    public static synchronized String readFile(String filePath) throws IOException {
        byte[] data = new byte[0];

        FileInputStream fileInputStream = null;

        try {//ww w . j  a va 2s.co m
            File file = new File(filePath);
            fileInputStream = new FileInputStream(new File(filePath));
            data = new byte[(int) file.length()];
            fileInputStream.read(data);
        } finally {
            closeResource(fileInputStream);
        }

        return new String(data, "UTF-8");
    }

    public static void closeResource(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

Related

  1. readFile(String filename)
  2. readFile(String filename)
  3. readFile(String filename)
  4. readFile(String fileName)
  5. readFile(String fileName, String charset)
  6. readFile(String filePath)
  7. readFile(String filePath)
  8. readFile(String filePath)
  9. readFile(String filePath)