Java FileInputStream Read readFile(String filename)

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

Description

read File

License

Open Source License

Declaration

public static String readFile(String filename) throws IllegalArgumentException 

Method Source Code


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

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

public class Main {
    public static String readFile(String filename) throws IllegalArgumentException {
        String fileContents = "";

        File file = new File(filename);
        if (!file.exists() || !file.isFile())
            throw new IllegalArgumentException();

        try (FileInputStream stream = new FileInputStream(file)) {
            byte[] fileBytes = new byte[(int) file.length()];

            stream.read(fileBytes, 0, (int) file.length());
            //throw new IllegalArgumentException();

            fileContents = new String(fileBytes);
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
            System.exit(-1);// w  w w .  j a  va 2  s. c o  m
        }

        return fileContents;
    }
}

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)
  8. readFile(String fileName, String charset)
  9. readFile(String filePath)