Commons IO - IOUtils

IOUtils is a general IO stream manipulation utilities. This class provides static utility methods for input/output operations.

  • closeQuietly - these methods close a stream ignoring nulls and exceptions
  • toXxx/read - these methods read data from a stream
  • write - these methods write data to a stream
  • copy - these methods copy all the data from one stream to another
  • contentEquals - these methods compare the content of two streams

 

Constant Field Values

 Modifier and Type  Field Description
 static char  DIR_SEPARATOR  The system directory separator character.
 static char  DIR_SEPARATOR_UNIX  The Unix directory separator character.
 static char  DIR_SEPARATOR_WINDOWS  The Windows directory separator character. 
 static int  EOF  Represents the end-of-file (or stream).
 static String  LINE_SEPARATOR  The system line separator string.
 static String  LINE_SEPARATOR_UNIX  The Unix line separator string.
 static String  LINE_SEPARATOR_WINDOWS   The Windows line separator string.

 

Common Methods

closeQuietly: Close a stream ignoring nulls and exceptions.

InputStream in = ...;
try {
    // do something with the stream
} finally {
    IOUtils.closeQuietly(in);
}

readLines: Gets the contents of an InputStream as a list of Strings, one entry per line.

List<String> lines = IOUtils.readLines(in);

toByteArray: Gets the contents of an InputStream(, String, URL) as a byte[].

byte[] byteArray = IOUtils.toByteArray(new URL("http://www.example.com/"));

toCharArray: Gets the contents of an InputStream(, Reader) as a character array.

char[] byteArray = IOUtils.toCharArray(in);

toInputStream: Converts the specified string(, CharSequence) to an input stream.

InputStream in = IOUtils.toInputStream("string");

toString: Gets the contents of an InputStream(, byte[], URL...) as a String

String str = IOUtils.toString(in);

 

更多请参考:http://commons.apache.org/proper/commons-io/javadocs/api-release/index.html?org/apache/commons/io/IOUtils.html

posted on 2016-05-12 11:21  huey2672  阅读(502)  评论(0编辑  收藏  举报