分類
|
類型
|
說明
|
備註
|
文件相關IO流
|
FileInputStream |
文件字節的輸入流
|
讀取任意類型的文件
|
FileOutputStream
|
文件字節的輸出流
|
可以把數據寫到任意類型的文件
|
|
FileReader
|
文件字符的輸入流
|
只能讀取純文本文件
|
|
FileWriter
|
文件字符的輸出流
|
只能把數據寫到純文本的文件
|
|
緩衝流
|
BufferedInputStream
|
字節緩衝的輸入流
|
給InputStream系列的IO流增加緩衝功能
|
BufferedOutputStream
|
字節緩衝的輸出流
|
給OutputStream系列的IO流增加緩衝功能
|
|
BufferedReader
|
字符緩衝的輸入流
|
給Reader系列的IO流增加緩衝功能
|
|
BufferedWriter
|
字符緩衝的輸出流
|
給Writer系列的IO流增加緩衝功能
|
|
數據IO流
|
DataInputStream
|
數據字節輸入流
|
|
DataOutputStream
|
數據字節輸出流
|
|
|
字符串IO流
|
StringReader
|
字符串輸入流
|
|
StringWriter
|
字符串輸出流
|
|
|
數組IO流
|
ByteArrayInputStream
|
從byte[]數組中讀取
|
|
ByteArrayOutputStram
|
寫到byte[]數組
|
|
|
CharArrayReader
|
從char[]數組中讀取
|
|
|
CharArrayWriter
|
寫入char[]數組
|
|
|
對象IO流
|
ObjectInputStream
|
對象字節輸入流
|
反序列化(反序列化就是把字節序列解析爲java對象)
|
ObjectOutputStram
|
對象字節輸出流
|
序列化(序列化的意思就是把java對象轉爲字節序列)
|
|
打印流
|
PrintStream
|
字節輸出
|
|
PrintWriter
|
字符輸出
|
|
-
關閉IO流,在流使用完成後要關閉流
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();
}
}