分类
|
类型
|
说明
|
备注
|
文件相关IO流
|
FileInputStream |
文件字节的输入流
|
读取任意类型的文件
|
File
OutputStream
|
文件字节的输出流
|
可以把数据写到任意类型的文件
|
|
FileR
eader
|
文件字符的输入流
|
只能读取纯文本文件
|
|
File
Writer
|
文件字符的输出流
|
只能把数据写到纯文本的文件
|
|
缓冲流
|
Buffered
InputStream
|
字节缓冲的输入流
|
给InputStream系列的IO流增加缓冲功能
|
Buffered
OutputStream
|
字节缓冲的输出流
|
给OutputStream系列的IO流增加缓冲功能
|
|
Buffered
R
eader
|
字符缓冲的输入流
|
给Reader系列的IO流增加缓冲功能
|
|
Buffered
Writer
|
字符缓冲的输出流
|
给Writer系列的IO流增加缓冲功能
|
|
数据IO流
|
DataInputStream
|
数据字节输入流
|
|
DataOutputStream
|
数据字节输出流
|
|
|
字符串IO流
|
StringReader
|
字符串输入流
|
|
StringWriter
|
字符串输出流
|
|
|
数组IO流
|
ByteArrayInputStream
|
从byte[]数组中读取
|
|
ByteArrayOutputStram
|
写到byte[]数组
|
|
|
CharArrayReader
|
从char[]数组中读取
|
|
|
CharArrayWriter
|
写入char[]数组
|
|
|
对象IO流
|
ObjectInputStream
|
对象字节输入流
|
反序列化(反序列化就是把字节序列解析为java对象)
|
ObjectOutputStram
|
对象字节输出流
|
序列化(序列化的意思就是把java对象转为字节序列)
|
|
打印流
|
PrintStream
|
字节输出
|
|
PrintWriter
|
字符输出
|
|
public FileInputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null);
}
public FileInputStream(File file) throws FileNotFoundException {
String name = (file != null ? file.getPath() : null);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(name);
}
if (name == null) {
throw new NullPointerException();
}
if (file.isInvalid()) {
throw new FileNotFoundException("Invalid file path");
}
fd = new FileDescriptor();
fd.attach(this);
path = name;
open(name);
}
public FileInputStream(FileDescriptor fdObj) {
SecurityManager security = System.getSecurityManager();
if (fdObj == null) {
throw new NullPointerException();
}
if (security != null) {
security.checkRead(fdObj);
}
fd = fdObj;
path = null;
/*
* FileDescriptor is being shared by streams.
* Register this stream with FileDescriptor tracker.
*/
fd.attach(this);
}
public int read() throws IOException {
return read0();
}
private native int read0() throws IOException;
public int read(byte b[]) throws IOException {
return readBytes(b, 0, b.length);
}
private native int readBytes(byte b[], int off, int len) throws IOException;
public int read(byte b[], int off, int len) throws IOException {
return readBytes(b, off, len);
}
private native int readBytes(byte b[], int off, int len) throws IOException;
/**
* 复制一个文件
* @param srcPath 原文件的目录
* @param destPath 要复制到的文件目录
*/
public static void copyFile(String srcPath,String destPath) {
// 创建IO流
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(srcPath);
fos = new FileOutputStream(destPath);
// 对IO流进行包装,增加缓冲效果
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//对文件进行读写操作
byte[] data = new byte[1024];
int len;
while((len = bis.read(data)) != -1){
bos.write(data,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
//关闭IO流,这里需要注意,可以只关闭最外层的IO流,如要关闭全部则需要使用先创建后关闭的原则
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 复制一个文件
* @param srcPath 原文件的目录
* @param destPath 要复制到的文件目录
*/
public static void copyFile1(String srcPath,String destPath) {
try(
//声明资源,在此处声明后会自动释放这些资源
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destPath);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
){
//对文件进行读写操作
byte[] data = new byte[1024];
int len;
while((len = bis.read(data)) != -1){
bos.write(data,0,len);
}
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}