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.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class Main {
    private static String DEFAULT_ENCODE = "UTF-8";

    public static String generateString(InputStream stream, String encoding) {
        InputStreamReader reader = null;
        BufferedReader br = null;
        try {
            if (encoding == null) {
                encoding = DEFAULT_ENCODE;
            }
            reader = new InputStreamReader(stream, encoding);

        } catch (UnsupportedEncodingException e1) {
            reader = new InputStreamReader(stream);
        }
        br = new BufferedReader(reader);
        StringBuffer sb = new StringBuffer();
        try {
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line + "\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
}