Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;

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

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

import java.nio.charset.Charset;

import java.util.zip.GZIPInputStream;

public class Main {
    public static String toString(final File file) throws IOException {
        return toString(file, false, Charset.defaultCharset());
    }

    public static String toString(final File file, final Charset charset) throws IOException {
        return toString(file, false, charset);
    }

    public static String toString(final File file, final boolean gzip) throws IOException {
        return toString(file, gzip, Charset.defaultCharset());
    }

    public static String toString(final File file, final boolean gzip, final Charset charset) throws IOException {
        InputStream is = new FileInputStream(file);
        if (gzip)
            is = new GZIPInputStream(is);
        return toString(is, charset);
    }

    public static String toString(final InputStream is) throws IOException {
        return toString(is, Charset.defaultCharset());
    }

    public static String toString(final InputStream is, final Charset charset) throws IOException {
        try {
            final BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));
            final char[] buffer = new char[8192];
            final StringBuilder sb = new StringBuilder();
            int read;
            while ((read = reader.read(buffer, 0, buffer.length)) != -1) {
                sb.append(buffer, 0, read);
            }
            return sb.toString();
        } finally {
            is.close();
        }
    }
}