Java: convert an input stream to string








package test;

import java.io.*;
import java.util.Scanner;

public class StreamUtil {

     public static String toString(InputStream in) throws Throwable {
        Scanner scanner = null;
        String s = "";
        try {
            scanner = new Scanner(in, "UTF-8").useDelimiter("\\A");
            s = scanner.hasNext() ? scanner.next() : "";
        } finally {
            scanner.close();
        }
        return s;
    }

    public static void main(String[] args) throws Throwable {
        
        System.out.println(toString(new FileInputStream(new File("/tmp/1.txt"))));
    }

}



No comments:

Post a Comment