Java FileInputStream Read readFile(String fname)

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

Description

read File

License

Open Source License

Declaration

public static String readFile(String fname) 

Method Source Code


//package com.java2s;
/*//from   www  .j a  v a  2s . c om
 * Copyright (C) 2009 Emweb bvba, Leuven, Belgium.
 *
 * See the LICENSE file for terms of use.
 */

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

public class Main {
    public static String readFile(String fname) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            copy(new FileInputStream(fname), baos);
            return baos.toString("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Could not read file: " + fname, e);
        } catch (IOException e) {
            throw new RuntimeException("Could not read file: " + fname, e);
        }
    }

    public static void copy(InputStream is, OutputStream os) throws IOException {
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = is.read(buffer))) {
            os.write(buffer, 0, n);
        }
    }
}

Related

  1. readFile(String filePath)
  2. readFile(String filePath)
  3. readFile(String filePathName)
  4. readFile(String fname)
  5. readFile(String fname)
  6. readFile(String imageName)
  7. readFile(String name)
  8. readFile(String path)
  9. Readfile(String path)