FileInputStream類(lèi)的使用:讀取文件內(nèi)容
package com.app;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class A1 {
public static void main(String[] args) {
A1 a1 = new A1();
//電腦d盤(pán)中的abc.txt 文檔
String filePath = "D:/abc.txt" ;
String reslut = a1.readFile( filePath ) ;
System.out.println( reslut );
}
/**
* 讀取指定文件的內(nèi)容
* @param filePath : 文件的路徑
* @return 返回的結(jié)果
*/
public String readFile( String filePath ){
FileInputStream fis=null;
String result = "" ;
try {
// 根據(jù)path路徑實(shí)例化一個(gè)輸入流的對(duì)象
fis = new FileInputStream( filePath );
//2. 返回這個(gè)輸入流中可以被讀的剩下的bytes字節(jié)的估計(jì)值;
int size = fis.available() ;
//3. 根據(jù)輸入流中的字節(jié)數(shù)創(chuàng)建byte數(shù)組;
byte[] array = new byte[size];
//4.把數(shù)據(jù)讀取到數(shù)組中;
fis.read( array ) ;
//5.根據(jù)獲取到的Byte數(shù)組新建一個(gè)字符串,然后輸出;
result = new String(array);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result ;
}
}
FileOutputStream 類(lèi)的使用:將內(nèi)容寫(xiě)入文件
package com.app;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class A2 {
public static void main(String[] args) {
A2 a2 = new A2();
//電腦d盤(pán)中的abc.txt 文檔
String filePath = "D:/abc.txt" ;
//要寫(xiě)入的內(nèi)容
String content = "今天是2017/1/9,天氣很好" ;
a2.writeFile( filePath , content ) ;
}
/**
* 根據(jù)文件路徑創(chuàng)建輸出流
* @param filePath : 文件的路徑
* @param content : 需要寫(xiě)入的內(nèi)容
*/
public void writeFile( String filePath , String content ){
FileOutputStream fos = null ;
try {
//1、根據(jù)文件路徑創(chuàng)建輸出流
fos = new FileOutputStream( filePath );
//2、把string轉(zhuǎn)換為byte數(shù)組;
byte[] array = content.getBytes() ;
//3、把byte數(shù)組輸出;
fos.write( array );
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
注意:
在實(shí)際的項(xiàng)目中,所有的IO操作都應(yīng)該放到子線程中操作,避免堵住主線程。
FileInputStream在讀取文件內(nèi)容的時(shí)候,我們傳入文件的路徑("D:/abc.txt"), 如果這個(gè)路徑下的文件不存在,那么在執(zhí)行readFile()方法時(shí)會(huì)報(bào)FileNotFoundException異常。
FileOutputStream在寫(xiě)入文件的時(shí)候,我們傳入文件的路徑("D:/abc.txt"), 如果這個(gè)路徑下的文件不存在,那么在執(zhí)行writeFile()方法時(shí), 會(huì)默認(rèn)給我們創(chuàng)建一個(gè)新的文件。還有重要的一點(diǎn),不會(huì)報(bào)異常。
綜合練習(xí),實(shí)現(xiàn)復(fù)制文件,從D盤(pán)復(fù)制到E盤(pán)
package com.app;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class A3 {
public static void main(String[] args) {
A3 a2 = new A3();
//電腦d盤(pán)中的cat.png 圖片的路徑
String filePath1 = "D:/cat.png" ;
//電腦e盤(pán)中的cat.png 圖片的路徑
String filePath2 = "E:/cat.png" ;
//復(fù)制文件
a2.copyFile( filePath1 , filePath2 );
}
/**
* 文件復(fù)制
* @param filePath_old : 需要復(fù)制文件的路徑
* @param filePath_new : 復(fù)制文件存放的路徑
*/
public void copyFile( String filePath_old , String filePath_new){
FileInputStream fis=null ;
FileOutputStream fout = null ;
try {
// 根據(jù)path路徑實(shí)例化一個(gè)輸入流的對(duì)象
fis = new FileInputStream( filePath_old );
//2. 返回這個(gè)輸入流中可以被讀的剩下的bytes字節(jié)的估計(jì)值;
int size = fis.available() ;
//3. 根據(jù)輸入流中的字節(jié)數(shù)創(chuàng)建byte數(shù)組;
byte[] array = new byte[size];
//4.把數(shù)據(jù)讀取到數(shù)組中;
fis.read( array ) ;
//5、根據(jù)文件路徑創(chuàng)建輸出流
fout = new FileOutputStream( filePath_new ) ;
//5、把byte數(shù)組輸出;
fout.write( array );
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if ( fout != null ) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}