Java InputStreamReader Read readFile(String fileName)

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

Description

read File

License

Open Source License

Declaration

public static char[] readFile(String fileName) throws IOException 

Method Source Code


//package com.java2s;
/*/*from w  w w. ja v a  2  s .  c o  m*/
 * Copyright (c) 2012-2016 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD 3-clause license that
 * can be found in the LICENSE.txt file in the project root.
 */

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

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

import java.util.Arrays;

public class Main {
    public static char[] readFile(String fileName) throws IOException {
        return readFile(fileName, null);
    }

    public static char[] readFile(String fileName, String encoding) throws IOException {
        File f = new File(fileName);
        int size = (int) f.length();
        InputStreamReader isr;
        FileInputStream fis = new FileInputStream(fileName);
        if (encoding != null) {
            isr = new InputStreamReader(fis, encoding);
        } else {
            isr = new InputStreamReader(fis);
        }
        char[] data = null;
        try {
            data = new char[size];
            int n = isr.read(data);
            if (n < data.length) {
                data = Arrays.copyOf(data, n);
            }
        } finally {
            isr.close();
        }
        return data;
    }
}

Related

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