InputSteam & OutputSteam
- InputStream (InputStreamReader)
BufferedInputStream
: used as other input stream's wrapper.
ByteArrayInputStream
: new ByteArrayInputStream(byteContent), A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream.
FileInputStream
: new FileInputStream(path); read file
//seldom use
DataInputStream
ObjectInputStream
: Read object from stream.
PipedInputStream
:
PushbackInputStream
SequenceInputStream
:
- OutputStream (OutputStreamWriter)
BufferedOutputStream
ByteArrayOutputStream
FileOutputStream
PrintStream
: System.out
//seldom use
DataOutputStream
ObjectOutputStream
PipedOutputStream
通常用法
InputStream bin = null;
OutputStream bout = null;
byte[] buffer = new byte[1024]; //
int length;
try {
bin = SomeInputStream();
bout = SomeOutputStream();
while((length = bin.read(buffer)) != -1) {
bout.write(buffer, 0, length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bin.close();
bout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Reader & Writer
- Reader
BufferedReader
CharArrayReader
FileReader
StringReader
//较少使用
PipedReader
PushbackReader
- Writer
BufferedWriter
CharArrayWriter
FileWriter
PrintWriter
StringWriter
//少用
PipedWriter
一些说明:
PrintWriter & PrintStream 最大特点就是它们提供了格式化的功能。pw.format("%s world", "hello");
Commons IO
IOUtils
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
FileUtils
- writing to a file
- reading from a file
- make a directory including parent directories
- copying files and directories
- deleting files and directories
- converting to and from a URL
- listing files and directories by filter and extension
- comparing file content
- file last changed date
- calculating a checksum
FilenameUtils
- the prefix - C:\
- the path - dev\project\
- the full path - C:\dev\project\
- the name - file.txt
- the base name - file
- the extension - txt