获取 InputStream 并将其转换为String的简单方法。
添加commons-io-2.4.jar
import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils; public class StringFromFile { public static void main(String[] args) throws IOException { InputStream inputStream = StringFromFile.class.getResourceAsStream("2.txt"); String myString = IOUtils.toString(inputStream, "UTF-8"); System.out.println(myString); } }
来源: http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
时间: 2024-10-03 22:40:34