Java File to String readFileAsString(String fname)

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

Description

read File As String

License

Apache License

Declaration

public static String readFileAsString(String fname) throws IOException 

Method Source Code

//package com.java2s;
/**//  www .ja va  2s.  c  o  m
 * Copyright 2014 
 * SMEdit https://github.com/StarMade/SMEdit
 * SMTools https://github.com/StarMade/SMTools
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 **/

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static String readFileAsString(String fname) throws IOException {
        return new String(readFile(fname));
    }

    public static byte[] readFile(String fname) throws IOException {
        byte[] ret;
        try (FileInputStream fis = new FileInputStream(fname)) {
            ret = readStream(fis);
        }
        return ret;
    }

    public static byte[] readStream(InputStream is) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        copy(is, baos);
        return baos.toByteArray();
    }

    public static void copy(InputStream is, OutputStream os) throws IOException {
        if (!(is instanceof BufferedInputStream)) {
            is = new BufferedInputStream(is);
        }
        if (!(os instanceof BufferedOutputStream)) {
            os = new BufferedOutputStream(os);
        }
        for (;;) {
            int ch = is.read();
            if (ch == -1) {
                break;
            }
            os.write(ch);
        }
        os.flush();
    }
}

Related

  1. readFileAsString(String filePath)
  2. readFileAsString(String filePath)
  3. readFileAsString(String filePath)
  4. readFileAsString(String filePathname)
  5. readFileAsString(String fname)
  6. readFileAsString(String fpath)
  7. readFileAsString(String path)
  8. readFileAsString(String path)
  9. readFileAsString(String path)