java的IO流和檔案讀寫
阿新 • • 發佈:2018-12-01
IO流
- IO流分類
- 按資料流向分:輸入流、輸出流
- 按資料型別分:位元組流(x xInputStream 讀,xxOutputStream寫)、字元流(xxRead讀,xxWrite寫)
如果資料所在的檔案通過windows自帶的記事本開啟並能讀懂裡面的內容,就用字元流。其他用位元組流。
檔案操作
- File類的概述和方法介紹
- File類的概述
檔案和目錄路徑名的抽象表示形式 - 構造方法
File(String pathname): 根據一個路徑得到File物件
File(String parent, String child): 根據一個目錄和一個子檔案/目錄得到File物件
File(File parent, String child): 根據一個父File物件和一個子檔案/目錄得到File物件 - 建立功能
public boolean createNewFile(): 建立檔案 如果存在這樣的檔案,就不建立了
public boolean mkdir(): 建立資料夾 如果存在這樣的資料夾,就不建立了
public boolean mkdirs(): 建立資料夾,如果父資料夾不存在,會幫你創建出來
注意事項:如果你建立檔案或者資料夾忘了寫碟符路徑,那麼,預設在專案路徑下。 - 刪除功能
public boolean delete(): 刪除檔案或者資料夾
注意事項:Java中的刪除不走回收站。要刪除一個資料夾,請注意該資料夾內不能包含檔案或者資料夾 - 重新命名功能
public boolean renameTo(File dest): 把檔案重新命名為指定的檔案路徑
注意事項:如果路徑名相同,就是改名。如果路徑名不同,就是改名並剪下。 - 判斷功能
public boolean isDirectory(): 判斷是否是目錄
public boolean isFile(): 判斷是否是檔案
public boolean exists(): 判斷是否存在
public boolean canRead(): 判斷是否可讀
public boolean canWrite(): 判斷是否可寫
public boolean isHidden(): 判斷是否隱藏 - 獲取功能
public String getAbsolutePath(): 獲取絕對路徑
public String getPath(): 獲取相對路徑
public String getName(): 獲取名稱
public long length(): 獲取長度。位元組數
public long lastModified(): 獲取最後一次的修改時間,毫秒值
public String[] list(): 獲取指定目錄下的所有檔案或者資料夾的名稱陣列
public File[] listFiles(): 獲取指定目錄下的所有檔案或者資料夾的File陣列
IO流讀寫檔案
位元組流讀寫檔案
- 普通輸入輸出位元組流讀寫
- FileOutputStream三個write()方法
public void write(int b):寫一個位元組。
public void write(byte[] b):寫一個位元組陣列。
public void write(byte[] b,int off,int len):寫一個位元組陣列的一部分。 - FileInputStream兩種讀取方式
int read(): 一次讀取一個位元組。
int read(bytes[] b) : 一次讀取一個位元組陣列。 - 使用示例(複製檔案)
位元組流一次讀一個位元組
private static void copyFile_1() throws IOException {
// 複製檔案使用的時間是:89352毫秒
// 建立物件
FileInputStream fis = new FileInputStream("C:\\waiting for you.mp3") ;
FileOutputStream fos = new FileOutputStream("D:\\waiting for you.mp3") ;
// 一次讀取一個位元組
int by = 0 ;
while((by = fis.read()) != -1){
fos.write(by) ;
}
// 釋放資源
fos.close() ;
fis.close() ;
}
一次讀一個位元組陣列
private static void copyFile_2() throws IOException {
// 複製檔案使用的時間是:154毫秒
// 建立物件
FileInputStream fis = new FileInputStream("C:\\waiting for you.mp3") ;
FileOutputStream fos = new FileOutputStream("D:\\waiting for you.mp3") ;
// 一次讀取一個位元組陣列
byte[] bytes = new byte[1024] ;
int len = 0 ;
while((len = fis.read(bytes)) != -1){
fos.write(bytes, 0, len) ;
}
// 釋放資源
fos.close() ;
fis.close() ;
}
- 緩衝輸入輸出位元組流讀寫
- 相同方式使用緩衝流讀寫效率較高,常用的檔案讀寫操作使用這種方式。(一次讀取一個位元組陣列的效率遠高於一次讀一個位元組)
- BufferedInputStream讀取資料,BufferedOutputStream寫出資料
- 使用示例
一次讀取一個位元組
public static void copyFile_3() throws IOException {
// 複製檔案使用的時間是:920毫秒
// 建立高效流物件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\waiting for you.mp3")) ;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\waiting for you.mp3")) ;
// 一次讀取一個位元組
int by = 0 ;
while((by = bis.read()) != -1){
bos.write(by) ;
}
// 釋放資源
bos.close() ;
bis.close() ;
}
一次讀取一個位元組陣列
public static void copyFile_4() throws IOException {
// 複製檔案使用的時間是:53毫秒
// 建立高效流物件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\waiting for you.mp3")) ;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\waiting for you.mp3")) ;
// 一次讀取一個位元組陣列
byte[] bytes = new byte[1024] ;
int len = 0 ;
while((len = bis.read(bytes)) != -1){
bos.write(bytes, 0, len) ;
}
// 釋放資源
bos.close() ;
bis.close() ;
}
字元流讀寫檔案
- 字元流出現的原因:
由於位元組流操作中文不是特別方便,所以,java就提供了字元流。
字元流: 字元流 = 位元組流 + 編碼表。 - 轉換流(OutputStreamWriter、InputStreamReader)
public static void main(String[] args) throws IOException {
// 建立轉換輸入流物件
InputStreamReader isr = new InputStreamReader(new FileInputStream("OutputStreamWriterDemo.java")) ;
// 建立轉換輸出流物件
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("copyFile.java")) ;
// 複製檔案
// 一次讀取一個字元複製
// int ch = 0 ;
// while((ch = isr.read()) != -1){
// osw.write(ch) ;
// }
// 一次讀取一個字元陣列複製檔案
char[] chs = new char[1024] ;
int len = 0 ;
while((len = isr.read(chs)) != -1){
osw.write(chs, 0, len) ;
}
// 釋放資源
osw.close() ;
isr.close() ;
}
- 高效流(FileWriter,FileReader)
public static void main(String[] args) throws IOException {
// 建立高效的字元輸入流物件
BufferedReader br = new BufferedReader(new FileReader("OutputStreamWriterDemo.java")) ;
// 建立高效的字元輸出流物件
BufferedWriter bw = new BufferedWriter(new FileWriter("copyFile3.java")) ;
// 一次讀取一個字元陣列複製檔案
char[] chs = new char[1024] ;
int len = 0;
while((len = br.read(chs)) != -1){
bw.write(chs, 0, len) ;
}
// 釋放資源
bw.close() ;
br.close() ;
}
- 緩衝流(BufferedWriter ,BufferedReader)
public static void main(String[] args) throws IOException {
/**
* 需求: 使用高效的字元流中特有的功能複製文字檔案
*/
// 建立高效的字元輸入流物件
BufferedReader br = new BufferedReader(new FileReader("OutputStreamWriterDemo.java")) ;
// 高效的字元輸出流物件
BufferedWriter bw = new BufferedWriter(new FileWriter("copyFile4.java")) ;
// 複製檔案
// 一次讀取一行復制檔案
String line = null ;
while((line = br.readLine()) != null) {
bw.write(line) ;
bw.newLine() ;
bw.flush() ;
}
// 釋放資源
bw.close() ;
br.close() ;
}
- 複製檔案工具類
public class IOUtils {
public static void copyFolder(String srcPahtName , String destPathName , FilenameFilter filenameFilter) throws IOException {
File srcFolder = new File(srcPahtName) ;
File destFolder = new File(destPathName) ;
if(!destFolder.exists()) {
destFolder.mkdir() ;
}
copyFolder(srcFolder , destFolder , filenameFilter) ;
}
public static void copyFolder(File srcFolder , File destFolder , FilenameFilter filenameFilter) throws IOException {
File[] files = null ;
if(filenameFilter == null) {
files = srcFolder.listFiles() ;
}else {
files = srcFolder.listFiles(filenameFilter) ;
}
// 遍歷
for(File f : files) {
// 建立目標檔案
String destFileName = f.getName() ;
File destFile = new File(destFolder , destFileName) ;
// 複製檔案
copyFile(f , destFile) ;
}
}
public static void copyFile(File srcFile , File destFile) throws IOException {
// 建立流物件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile)) ;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile)) ;
// 一次讀取一個位元組陣列複製檔案
byte[] bytes = new byte[1024] ;
int len = 0 ;
while((len = bis.read(bytes)) != -1){
bos.write(bytes, 0, len) ;
}
// 釋放資源
bos.close() ;
bis.close() ;
}
}
IO流分類
- 按操作方式分類結構圖
2. 按操作物件分類結構圖
參考文章:https://github.com/yangchong211/YCBlogs/blob/master/java/IO流知識