Java FileInputStream Read readFile(String resource)

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

Description

read File

License

Open Source License

Declaration

public static String readFile(String resource) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w w w . j  av a  2s.  co m
 Chromis POS  - The New Face of Open Source POS
 Copyright (c) 2015 (John Lewis) Chromis.co.uk
    
 http://www.chromis.co.uk
    
 kitchen Screen v1.5
    
 This file is part of chromis & its associated programs
    
 chromis is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 chromis is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with chromis.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Main {
    public static String readFile(String resource) throws IOException {

        FileInputStream in = new FileInputStream(resource);
        if (in == null) {
            throw new FileNotFoundException(resource);
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        byte[] data = out.toByteArray();

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

Related

  1. readFile(String path, long offset)
  2. readFile(String path, Properties store)
  3. readFile(String path, String encoding)
  4. readFile(String path, String root, OutputStream out)
  5. readFile(String pPathToFile)
  6. readFile(String sFileName)
  7. readFileAsBinary(File file)
  8. readFileAsInputStream(File file)
  9. readFileAsInputStream(String fileName)