获取InputStream 并将其转换为String的简单方法。 添加commons-io-2.4.jar import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils;publicclassStringFromFile {publicstaticvoidmain(String[] args) throws IOException { InputStream inputStream= StringFromFile.class.getReso...
public String getContent(final URL url) { try { InputStream inputStream = url.openStream(); return new Scanner(inputStream).useDelimiter("\\A").next(); } catch (final Exception e) { } return ""; } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11....
InputStream source = new URL("http://pat.net/misc/foo.txt").openStream(); The canonical way to gather it to a String has always been to use a BufferedReader, e.g. BufferedReader br = new BufferedReader( new InputStreamReader( source ) ); StringBuffer text = new StringBuffer(); fo...
How to convert an InputStream to a stringBrian L. Gorman
1. UsingInputStream.readAllBytes()(Since Java 9) TheInputStream.readAllBytes()API converts the input stream to bytes. Then we use thenew String()to create a newStringobject. InputStreamin=newFileInputStream(newFile("C:/temp/test.txt"));StringfileContent=newString(in.readAllBytes()); ...
If you have a java.io.InputStream object, how should you process that object and produce a String? Suppose I have an InputStream that contains text data, and I want to convert it to a String, so for example I can write that to a log file. PartyCity...
1. UsingByteArrayInputStream UsingByteArrayInputStreamis the simplest way to createInputStreamfrom aString. Using this approach, we do not need any external dependency. Thestring.getBytes()method encodes theStringinto a sequence of bytes using the platform’s default charset. To use a different ch...
public static void convertInputStreamToFileNio(InputStream is) throws IOException { String outputFile = "C:\\Users\\user\\Desktop\\test\\output.txt"; Files.copy(is, Paths.get(outputFile)); File file = new File(outputFile); } 3- Apache commons library Apart from JDK, you can use apa...
any equivalent in c# for bytearray outputstream/inputstream and data outputstream/inputstream? App Config and escape sequences App Config key not working App setting inacessible due to protection level App.config for multiple groups of same key/value pairs App.config for release and another for ...
字符串".getBytes()); String result = IOUtil.convertToString...); System.out.println(result); } } Java 9代码 在Java 9 发布之后,我们决定使用Java 9 的新的语法重写IOUtil.convertToString...package com.example; public class IOUtil { public static String convertToString(InputStream inputStream...