InputStream 和 Reader 是所有輸入流的基類。
InputStream(典型實現:FileInputStream)
int read()
int read(byte[] b)
int read(byte[] b, int off, int len)
Reader(典型實現:FileReader)
int read()
int read(char [] c)
int read(char [] c, int off, int len)
程序中打開的文件 IO 資源不屬于內存里的資源,垃圾回收機制無法回收該資源,所以應該顯式關閉文件 IO 資源。
package com.atguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
/*
* 1.流的分類
* 按照數據流向的不同:輸入流、輸出流
* 按照處理數據的單位的不同:字節流 字符流(處理的文本文件)
* 按照角色的不同:字節流(直接作用于文件) 處理流
* 2.IO的體系
* 抽象基類 節點流(文件流) 緩沖流
* InputStream FileInputStream BufferedInputStream
* OutputStream FileOutputStream BufferedOutputStream
* Reader FileReader BufferedReader
* Writer FileWriter BufferedWriter
*/
public class TestFileInputOutputStream {
//調用下面的復制方法,并計算復制時間
@Test
public void testCopyFile(){
long start = System.currentTimeMillis();//毫秒
String src = "C:\\Users\\xiaotinh\\Desktop\\03.jpg";
String dest = "C:\\Users\\xiaotinh\\Desktop\\04.jpg";
copyFile(src, dest);
long end = System.currentTimeMillis();
System.out.println("花費時間為:"+ (end - start));
}
//實現文件復制的方法,加了參數的方法具有通用性
public void copyFile(String src,String dest){
//1.提供讀入、寫出的文件
File file1 = new File(src);
File file2 = new File(dest);
//2.提供相應的流
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
//3.實現文件的復制
byte[] b = new byte[20];
int len;
while((len = fis.read(b)) != -1){
//fos.write(b);錯誤寫法 fos.write(b,0,b.length)是一樣的
fos.write(b, 0, len);//寫b里面的東西;從0開始;len是長度
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(fos!= null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis!= null){//fis和fos的關閉可以并列進行,因為都處于finally中,最后都會執行
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//從硬盤讀取一個文件,并寫入到另一個位置。(相當于文件的復制)
@Test
public void testFileInputOutputStream(){
//1.提供讀入、寫出的文件
File file1 = new File("hello.txt");
File file2 = new File("hello3.txt");
//2.提供相應的流
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
//3.實現文件的復制
byte[] b = new byte[20];
int len;
while((len = fis.read(b)) != -1){
//fos.write(b);錯誤寫法 fos.write(b,0,b.length)是一樣的
fos.write(b, 0, len);//寫b里面的東西;從0開始;len是長度
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(fos!= null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis!= null){//fis和fos的關閉可以并列進行,因為都處于finally中,最后都會執行
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//FileOutputStream
@Test
public void testFileOutputStream(){
//1.創建一個File對象,標明要寫入的文件位置
//輸出的物理文件可以不存在,當執行過程中,若不存在,會自動創建。若存在,會將原有的文件覆蓋。
File file = new File("hello2.txt");
//2.創建一個FileOutputStream的對象,將file的對象作為形參傳遞給FileOutputStream的構造器中
FileOutputStream fos = null;
try{
fos = new FileOutputStream(file);
//3.寫入操作
fos.write(new String("I love China I love the World!").getBytes());
}catch(IOException e){
e.printStackTrace();
}finally{
//4.關閉輸出流
if(fos != null){
try{
fos.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testFileInputOutputStream3(){
FileInputStream fis = null;
try {
File file = new File("hello.txt");
fis = new FileInputStream(file);
byte[] b = new byte[5];//讀取到的數據要寫入的數組
int len;//每次讀到byte中的字節的長度
while((len = fis.read(b)) != -1){
// for (int i = 0; i < len; i++) {//不可以寫成b.length,否則輸出abcdefgcde
// System.out.print((char)b[i]);
// }
String str = new String(b, 0, len);
System.out.print(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fis!= null){//可以判斷fis不為空時,才執行.close()
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//使用try-catch的方式處理如下的異常更合理!
//因為throw是遇到問題就拋出,下面的程序便不再執行,所以無法釋放打開的珍貴的流資源
//而try-catch則會在抓到異常以后,繼續執行,而將.close放到finally中,則必然會被關閉
@Test
public void testFileInputOutputStream2(){
//2.創建一個FileInputStream類的對象
FileInputStream fis= null;
try {
//1.創建一個File類的對象
File file = new File("hello.txt");
fis = new FileInputStream(file);
//3.調用FileInputStream的方法,實現file問價的讀取
int b;
while((b = fis.read()) != -1){
System.out.print((char)b);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
//4.關閉相應的流
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//因為流的資源并不是java虛擬機的資源,不會自動回收,需要顯示關閉
}
}
//從硬盤存在的文件中,讀取其內容到程序中。使用FileInputStream
//要讀取的文件一定要存在,否則拋FileNotFoundException
@Test
public void testFileInputOutputStream1() throws Exception{
//1.創建一個File類的對象
File file = new File("hello.txt");
//2.創建一個FileInputStream類的對象
FileInputStream fis = new FileInputStream(file);
//3.調用FileInputStream的方法,實現file問價的讀取
/*
* read():讀取文件的一個字節。 當執行到文件結尾時,返回-1.abcdefg
*/
// int b = fis.read();
// while(b != -1){
// System.out.print((char)b);
// b = fis.read();
// }或者
int b;
while((b = fis.read()) != -1){
System.out.print((char)b);
}
//4.關閉相應的流
fis.close();//因為流的資源并不是java虛擬機的資源,不會自動回收,需要顯示關閉
}
}