構造方法 |
說明 |
FileInputStream(File file) |
根據給定的File實例構建流對象 |
FileInputStream(String name) |
根據給定的文件名構建流對象 |
方法 |
功能 |
返回值 |
read() |
讀取一個字節 |
int |
read(byte[]) |
讀取多個字節到字節數組中 |
int |
FileOutputStream
構造方法 |
說明 |
FileOutputStream(File file) |
創建一個向指定 File 對象表示的文件中寫入數據的文件輸出流 |
FileOutputStream(File file, boolean append) |
創建一個向指定 File 對象表示的文件中寫入數據的文件輸出流 ,指示釋放追加 |
FileOutputStream(String name) |
創建一個向具有指定名稱的文件中寫入數據的輸出文件流 |
FileOutputStream(String name, boolean append) |
創建一個向具有指定名稱的文件中寫入數據的輸出文件流,指示是否追加 |
方法 |
功能 |
返回值 |
write() |
將指定字節寫入此文件輸出流 |
void |
write(byte[] b) |
將 b.length 個字節從指定 byte 數組寫入此文件輸出流中 |
void |
write(byte[] b, int off, int len) |
將指定 byte 數組中從偏移量 off 開始的 len 個字節寫入此文件輸出流 |
void |
available() |
返回未讀字節數 |
int |
拷貝文件示例
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream("E:/apache-tomcat-8.5.9-windows-x64.zip");
output = new FileOutputStream("D:/temp.zip");
byte[] buffer = new byte[1024 * 8];
int len;
while((len = input.read(buffer)) > 0) {
output.write(buffer, 0, len);
}
System.out.println("Done.");
}catch(IOException e) {
e.printStackTrace();
}finally {
try{
if(input != null) {
input.close();
}
}catch(IOException e) {
e.printStackTrace();
}finally {
try{
if(output != null) {
output.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}