Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5663c29

Browse files
committed
read-convert-an-inputstream-to-a-string
1 parent 9b53ad7 commit 5663c29

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

‎README.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ stackoverflow-Java-top-qa
1010
1111
* [Java是按值传递还是按引用传递](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-java-pass-by-reference-or-pass-by-value.md)
1212
* [Java += 操作符实质](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/java-operator.md)
13-
13+
*[将InputStream转换为String](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/read-convert-an-inputstream-to-a-string.md)
1414

1515

1616
> 代码规范
@@ -30,7 +30,6 @@ stackoverflow-Java-top-qa
3030
- [Why is subtracting these two times (in 1927) giving a strange result?](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result)
3131
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
3232
- [Differences between HashMap and Hashtable?](http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable)
33-
- [Read/convert an InputStream to a String](http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string)
3433
- [Creating a memory leak with Java [closed]](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java)
3534
- [Iterate through a HashMap [duplicate]](http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap)
3635
- [Create ArrayList (ArrayList<T>) from array (T[])](http://stackoverflow.com/questions/157944/create-arraylist-arraylistt-from-array-t)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
##将InputStream转换为String
2+
3+
###使用Apache库
4+
不重复造轮子。最靠谱的方法,还是用Apache commons IOUtils
5+
这样简单几行代码就搞定了
6+
```java
7+
StringWriter writer = new StringWriter();
8+
IOUtils.copy(inputStream, writer, encoding);
9+
String theString = writer.toString();
10+
```
11+
或者
12+
String theString = IOUtils.toString(inputStream, encoding)//这个方法其实封装了上面的方法,减少了一个参数
13+
14+
###使用原生库
15+
如果不想引入Apache库,也可以这样做
16+
```java
17+
static String convertStreamToString(java.io.InputStream is) {
18+
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
19+
return s.hasNext() ? s.next() : "";
20+
}
21+
```
22+
23+
stackoverflow讨论地址
24+
http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /