Java UTF8 InputStream Read LoadUTF8(InputStream stream)

Here you can find the source of LoadUTF8(InputStream stream)

Description

Load UTF

License

Open Source License

Declaration

public static String LoadUTF8(InputStream stream) throws UnsupportedEncodingException, IOException 

Method Source Code


//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.io.UnsupportedEncodingException;

public class Main {
    public static String LoadUTF8(InputStream stream) throws UnsupportedEncodingException, IOException {

        BufferedReader in = null;
        try {/*  w  w  w  .  ja  va  2  s.c  o m*/
            in = new BufferedReader(new InputStreamReader(stream, "UTF8"));

            String str;
            StringBuilder builder = new StringBuilder();

            while ((str = in.readLine()) != null) {
                builder.append(str);
                builder.append("\n");
            }
            return builder.toString();

        } finally {
            if (in != null) {
                in.close();
            }
        }
    }

    public static String LoadUTF8(String filename) throws UnsupportedEncodingException, IOException {

        BufferedReader in = null;
        try {
            File fileDir = new File(filename);
            in = new BufferedReader(new InputStreamReader(new FileInputStream(fileDir), "UTF8"));

            String str;
            StringBuilder builder = new StringBuilder();

            while ((str = in.readLine()) != null) {
                builder.append(str);
                builder.append("\n");
            }
            return builder.toString();

        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
}

Related

  1. loadUTF8Stream(InputStream stream)