File tree Expand file tree Collapse file tree 4 files changed +110
-0
lines changed 
src/main/java/com/hmkcode Expand file tree Collapse file tree 4 files changed +110
-0
lines changed Original file line number Diff line number Diff line change 1+ <?xml  version =" 1.0"  encoding =" UTF-8"  ?>
2+ <classpath >
3+   <classpathentry  kind =" src"   path =" src/test/java"   output =" target/test-classes"   including =" **/*.java"  />
4+   <classpathentry  kind =" src"   path =" src/main/java"   including =" **/*.java"  />
5+   <classpathentry  kind =" output"   path =" target/classes"  />
6+   <classpathentry  kind =" con"   path =" org.eclipse.jdt.launching.JRE_CONTAINER"  />
7+   <classpathentry  kind =" var"   path =" M2_REPO/commons-io/commons-io/2.4/commons-io-2.4.jar"  />
8+   <classpathentry  kind =" var"   path =" M2_REPO/com/google/guava/guava/14.0.1/guava-14.0.1.jar"  />
9+ </classpath >
Original file line number Diff line number Diff line change 1+ <project  xmlns =" http://maven.apache.org/POM/4.0.0"   xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" 
2+   xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"  >
3+   <modelVersion >4.0.0</modelVersion >
4+ 
5+   <groupId >com.hmkcode</groupId >
6+   <artifactId >java-inputstream-string</artifactId >
7+   <version >1.0-SNAPSHOT</version >
8+   <packaging >jar</packaging >
9+ 
10+   <name >java-inputstream-string</name >
11+   <url >http://maven.apache.org</url >
12+ 
13+   <properties >
14+     <project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
15+   </properties >
16+ 
17+   <dependencies >
18+     <dependency >
19+ 		<groupId >commons-io</groupId >
20+ 		<artifactId >commons-io</artifactId >
21+ 		<version >2.4</version >
22+ 	</dependency >
23+ 	<dependency >
24+ 		<groupId >com.google.guava</groupId >
25+ 		<artifactId >guava</artifactId >
26+ 		<version >14.0.1</version >
27+ 	</dependency >
28+   </dependencies >
29+ </project >
Original file line number Diff line number Diff line change 1+ package  com .hmkcode ;
2+ 
3+ import  java .io .ByteArrayInputStream ;
4+ import  java .io .IOException ;
5+ import  java .io .InputStream ;
6+ 
7+ public  class  App  
8+ {
9+     public  static  void  main ( String [] args  ) throws  IOException 
10+     {
11+     	
12+         // 1. By java.util.Scanner 
13+     	InputStream  inputStream  = new  ByteArrayInputStream ("hmkcode..inputStream to String" .getBytes ());
14+     	System .out .println ("getStringByScanner: " +InputStreamToString .getStringByScanner (inputStream ));
15+     	
16+         // 2. By BufferedReader 
17+     	inputStream  = new  ByteArrayInputStream ("hmkcode..inputStream to String" .getBytes ());
18+     	System .out .println ("getStringByBufferedReader: "  +InputStreamToString .getStringByBufferedReader (inputStream ));
19+ 
20+         // 3. By Apache IO 
21+     	inputStream  = new  ByteArrayInputStream ("hmkcode..inputStream to String" .getBytes ());
22+     	System .out .println ("getStringByApacheIO IOUtils.toString: " +InputStreamToString .getStringByApacheIO_toString (inputStream ));
23+     	
24+         // 4. By Apache IO 
25+     	inputStream  = new  ByteArrayInputStream ("hmkcode..inputStream to String" .getBytes ());
26+     	System .out .println ("getStringByApacheIO IOUtils.copy: " +InputStreamToString .getStringByApacheIO_copy (inputStream ));
27+     	
28+         
29+     }
30+     
31+ }
Original file line number Diff line number Diff line change 1+ package  com .hmkcode ;
2+ 
3+ import  java .io .BufferedReader ;
4+ import  java .io .IOException ;
5+ import  java .io .InputStream ;
6+ import  java .io .InputStreamReader ;
7+ import  java .io .StringWriter ;
8+ import  java .util .Scanner ;
9+ import  org .apache .commons .io .IOUtils ;
10+ 
11+ public  class  InputStreamToString  {
12+ 
13+     // 1. By java.util.Scanner 
14+     public  static  String  getStringByScanner (InputStream  inputStream ){
15+     	return  new  Scanner (inputStream ,"UTF-8" ).next ();
16+     }
17+     
18+     // 2. By BufferedReader 
19+     public  static  String  getStringByBufferedReader (InputStream  inputStream ) throws  IOException {
20+     	BufferedReader  bufferedReader  = new  BufferedReader ( new  InputStreamReader (inputStream ));
21+     	String  line  = "" ;
22+     	String  result  = "" ;
23+     	while ((line  = bufferedReader .readLine ()) != null )
24+     		result  += line ;
25+     	return  result ;
26+     }
27+     
28+     // 3. By Apache IO 
29+     public  static  String  getStringByApacheIO_toString (InputStream  inputStream ) throws  IOException {
30+     	return  IOUtils .toString (inputStream );
31+     }
32+     
33+     // 4. By Apache IO 
34+     public  static  String  getStringByApacheIO_copy (InputStream  inputStream ) throws  IOException {
35+     	StringWriter  writer  = new  StringWriter ();
36+         IOUtils .copy (inputStream , writer , "UTF-8" );
37+         return  writer .toString ();
38+     }
39+     
40+ 
41+ }
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments