Java Text File Read by Encode getFileContentAsString(File file, String encoding)

Here you can find the source of getFileContentAsString(File file, String encoding)

Description

get File Content As String

License

Open Source License

Declaration

static public String getFileContentAsString(File file, String encoding) throws Exception 

Method Source Code


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

import java.io.BufferedInputStream;

import java.io.ByteArrayOutputStream;

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

import java.io.IOException;
import java.io.InputStream;

public class Main {
    static public String getFileContentAsString(File file, String encoding) throws Exception {
        FileInputStream is = new FileInputStream(file);
        return new String(getStreamContentAsBytes(is), encoding);
    }/*  w  w w  .  j av  a 2  s . co  m*/

    static public String getFileContentAsString(File file) throws Exception {
        FileInputStream is = new FileInputStream(file);
        return new String(getStreamContentAsBytes(is));
    }

    static public String getFileContentAsString(String fileName, String encoding) throws Exception {
        if (fileName == null)
            return null;
        FileInputStream is = new FileInputStream(fileName);
        return new String(getStreamContentAsBytes(is), encoding);
    }

    static public String getFileContentAsString(String fileName) throws Exception {
        FileInputStream is = new FileInputStream(fileName);
        String data = new String(getStreamContentAsBytes(is));
        is.close();
        return data;
    }

    static public byte[] getStreamContentAsBytes(InputStream is) throws IOException {
        BufferedInputStream buffer = new BufferedInputStream(is);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] data = new byte[4912];
        int available = -1;
        while ((available = buffer.read(data)) > -1) {
            output.write(data, 0, available);
        }
        is.close();
        return output.toByteArray();
    }

    static public byte[] getStreamContentAsBytes(InputStream is, int maxRead) throws IOException {
        BufferedInputStream buffer = new BufferedInputStream(is);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] data = new byte[4912];
        int available = -1, read = 0;
        while ((available = buffer.read(data)) > -1 && read < maxRead) {
            if (maxRead - read < available)
                available = maxRead - read;
            output.write(data, 0, available);
            read += available;
        }
        return output.toByteArray();
    }
}

Related

  1. getFileContent(File file, String charsetName)
  2. getFileContent(FileInputStream fis, String encoding)
  3. getFileContent(InputStream is, String encoding)
  4. getFileContentAsString(File file, String encoding)
  5. getFileContentAsString(File file, String encoding)
  6. getFileContentAsString(String filePath, String fileEncoding)
  7. getFileContents(InputStream stream, String encoding)
  8. getFileContents(String filename, String encoding)
  9. readFileAsString(final File f, final String encoding)