------Java培訓、Android培訓、iOS培訓、.Net培訓、期待與您交流! -------
IO 流
常用基類
字節流 InputStreamOutputStream
常用方法
int read() // 讀取一個字節,以 int 形式返回
intread(byte[] b) // 讀取最多數組長度個字節並存入該數組,返回實際讀取字節
void write(int d) // 寫出一個字節
voidwrite(byte[] d) // 將給定字節數組中的所有字節全部寫出
void write(byte[] d, int off, int len) // 將字節數組中指定部分寫出
字符流 ReaderWriter
常用方法
int read() // 讀取一個字符,以 int 形式返回
intread(char[] chs) // 讀取最多數組長度個字符並存入數組返回讀取有效字符
void wirte(int c) // 寫出一個字符
void write(char[]chs) // 將給定字符數組中所有字符寫出
void write(char[] chs, int offset, int len) // 將字符數組中指定部分寫出
voidwirte(String str) // 將給定的字符串寫出
voidwirte(String str, int off, int len) // 將字符串中指定部分寫出
緩衝字符流
BufferedWriter 緩衝字符輸出流
提高字節輸出流的效率,繼承 OutputStream
BufferedWriter(Writer writer)
void newLine() // 寫出新換行,具有跨平臺特性
BufferedReader 緩衝字符輸入流
BufferedReader(Reader reader)
String readLine() // 讀取一行字符串,直到讀取到換行符位置(不包含換行符)
// 創建FileReader對象,傳遞字符串文件名
FileReaderfr = new FileReader("C:\\BufferedDemo.txt");
// 創建緩衝字符輸入流對象,傳遞字符輸入流對象
BufferedReaderbr = new BufferedReader(fr);
// 讀取一個文本行
Stringlen= null;
while ((len = br.readLine()) != null) {
System.out.println(len);
}
br.close();
字符串的編碼和解碼
package cn.itcast;
/*
* 字符串編碼和解碼
* 字符串 --> 字節數組
* 字節數組 --> 字符串
*/
publicclassEncodingDemo {
publicstaticvoid main(String[] args) throws Exception {
// 字符串 --> 字節數組
byte[] bs = "你好".getBytes(); // gbk
for (byteb : bs) {
System.out.print(b + " "); // -60 -29 -70 -61
}
System.out.println();
byte[] bs2 = "你好".getBytes("utf-8");
for (byteb : bs2) {
System.out.print(b + " "); // -28 -67 -96 -27 -91 -67
}
System.out.println();
byte[] bs3 = "你好".getBytes("big5");
for (byteb : bs3) {
System.out.print(b + " "); // -89 65 -90 110
}
System.out.println();
// 字節數組 --> 字符串
byte[] bytes = {-60, -29, -70, -61};
Stringstring= newString(bytes);// gbk
System.out.println(string);
byte[] bytes2 = {-28, -67, -96, -27,-91, -67};
Stringstring2= newString(bytes2,"utf-8");
System.out.println(string2);
byte[] bytes3 = {-89, 65, -90, 110};
Stringstring3= newString(bytes3,"big5");
System.out.println(string3);
}
}
複製單級文件夾、修改名稱、複製多級文件夾
package cn.itcast;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
/*
* 複製單級文件夾
* 1.在目標路徑創建和源路徑同名文件夾
* 2.遍歷源路徑下的文件,並進行復制
* 3.改進功能:複製多級文件夾
* 4.增加複製文件後改名的功能
*/
publicclassCopyFileFolder {
publicstaticvoid main(String[] args) {
Scannersc = new Scanner(System.in);
System.out.println("請輸入源路徑");
Stringsour= sc.nextLine();
sour.replace("\\", "\\\\");
System.out.println("請輸入目標路徑");
Stringdest= sc.nextLine();
dest.replace("\\", "\\\\");
sc.close();
longstart = System.currentTimeMillis();
copyFolder(new File(sour), new File(dest));
System.out.println("複製完成!!");
longend = System.currentTimeMillis();
System.out.println("總共用時 " + (end - start)+ " ms");
}
// 傳遞源路徑和目標路徑
publicstaticvoid copyFolder(File sourPath, File destPath) {
// 獲取源文件夾名
StringfolderName= sourPath.getName();
// 根據目標路徑和源文件夾名創建file對象
FiledestFolder= newFile(destPath,folderName);
// 創建目標文件夾
destFolder.mkdirs(); // I:\Knownsec
System.out.println("創建文件夾 " + destFolder.getName());
// 遍歷源路徑獲取所有文件名和文件夾名
File[]files= sourPath.listFiles();
for (File file : files) {
// 判斷如果是文件,獲取文件名,進行復制操作
if (file.isFile()) {
// 獲取文件名字符串形式
StringfileName= file.getName();
// System.out.println(fileName);
// 根據目標父路徑和源文件名創建file對象
FiledestFile= newFile(destFolder,fileName);
// 傳遞源文件對象和目標文件對象進行復制操作
copyFile(file, destFile);
rename(destFile);
}else{ // 是文件夾,遞歸調用本函數
// 此時,源路徑爲當前文件夾名,目標路徑爲剛纔創建好的目標路徑下的文件夾
copyFolder(file, destFolder);
}
}
}
// 對文件進行重命名
publicstaticvoid rename(File destFile) {
// 獲取文件父路徑
Fileparent= destFile.getParentFile();
// 對文件名進行切割,方便獲取文件擴展名
String[]name= destFile.getName().split("\\.");
// 將文件名擴展名替換爲新的擴展名
Stringchild= destFile.getName().replace(name[name.length - 1], "bak");
// 根據父路徑和新文件名構造新文件對象,
FiledestNew= newFile(parent,child);
// 傳遞改名後的新文件對象
destFile.renameTo(destNew);
}
// 利用緩衝字節流實現讀寫字節數組實現複製文件
publicstaticvoid copyFile(File file, File destFile) {
// 聲明緩衝字節流對象
BufferedInputStreambis= null;
BufferedOutputStreambos= null;
try {
// 創建緩衝字節流對象,封裝字節流對象
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(new FileOutputStream(destFile));
longstart = System.currentTimeMillis();
byte[] b = newbyte[1024];
intlen = 0;
System.out.println("正在複製... " + file.getName() + " 文件大小約 " + (file.length()/1024)+ " KB");
// 利用字節數組複製讀寫字節流
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
longend = System.currentTimeMillis();
System.out.println("用時 " + (end- start)+ " ms");
}catch(IOException e) {
e.printStackTrace();
thrownew RuntimeException("複製失敗");
}finally{
try {
if (bos != null) {
bos.close();
}
}catch(IOException e) {
e.printStackTrace();
thrownew RuntimeException("關閉資源失敗");
}finally{
if (bis != null) {
try {
bis.close();
}catch(IOException e) {
e.printStackTrace();
thrownew RuntimeException("關閉資源失敗");
}
}
}
}
}
}
IO流使用規律總結
----------------------------
字節流
OutputStream
|-- FileOutputStream
|-- BufferedOutputStream
InputStream
|-- FileInputStream
|-- BufferedInputStream
----------------------------
字符流
Writer
|-- OutputStreamWriter
|-- FileWriter
|-- BufferedWriter
Reader
|-- InputStreamReader
|-- FileReader
|-- BufferedReader
----------------------------
明確數據源:
數據源是文本,選擇字符輸入流
需要指定編碼表InputStreamReader
不需要編碼表 FileReader
需要提高效率,字符數組緩衝BufferedReader 讀取文本行
數據源不是文本,數據源不明確,選擇字節輸入流
FileInputStream
需要提高效率,字節數組緩衝BufferedInputStream
明確數據目的:
數據目的是文本,選擇字符輸出流
需要指定編碼表OutputStreamWriter
不需要編碼表 FileWriter
需要提高效率,字符數組緩衝BufferedWriter 寫入文本行
數據目的不是文本,數據目的不明確,選擇字節輸出流
FileOutputStream
需要提高效率,字節數組緩衝BufferedOutputStream
操作基本數據類型的流
DataInputStream
DataOutputStream
內存操作流
一般用於處理臨時信息,不需要保存,使用後就可以刪除
只操作內存,不操作硬盤,不佔用 OS 資源
字節數組
ByteArrayInputStream
ByteArrayOutputStream
字符數組
CharArrayReader
CharArrayWriter
字符串
StringReader
StringWriter
打印流
java.io.PrintStream 繼承OutputStream
是具有自動行刷新的緩衝字符輸出流
爲其他輸出流添加功能,打印流也是裝飾流,不會拋出 IOException
只操作數據目的,不操作數據源
PrintWriter(File file) // 文件
PrintWriter(String fileName)
PrintWriter(OutputStream out) // 字節流
PrintWriter(OutputStream out, boolean autoFlush) // 自動行刷新
PrintWriter(Writer writer) // 字符流
PrintWriter(Writer writer, boolean autoFlush) // 自動行刷新
print 與 println 方法
PrintWriter 提供了豐富的重載 print 與 println 方法。其中 println 方法在於輸出目標數據後自動輸出一個系統支持的換行符。若該流是具有自動行刷新的,那麼每當通過 println 方法寫出的內容都會被實際寫出,而不是進行緩存。
可以操作任意類型的數據
void print(int/char/boolean/char[]/double/float/long/String)
標準輸入輸出流
OutputStream
|-- FileOutputStream
|-- PrintStream
|-- BufferedOutputStream
InputStream
|-- FileInputStream
|-- BufferedInputStream
System.in(InputStream), System.out(PrintStream)
隨機訪問流 RandomAccessFile
Java提供了一個可以對文件隨機訪問(讀和寫)的操作,該類不屬於流,是 Object 類的子類,但融合了InputStream 和OutputStream 的功能,該類的讀寫是基於指針的操作
RandomAccessFile(File file, String mode)
RandomAccessFile(String filename, String mode)
只讀模式 mode=r
讀寫模式 mode=rw
字節數據讀寫操作
voidwrite(int d) // 向文件中寫入一個字節, int 低8位
voidwrite(byte[] b) // 向文件中寫出給定數組
voidwrite(byte[] b, int offset, int len) // 向文件中寫出數組中指定部分
int read()// 從文件中讀取一個字節讀取一個byte 填充到int 低8位,高位補0
intread(byte[] b) // 從文件中讀取最多給定數組長度的字節兩
void close()// 釋放系統資源
文件指針操作
long getFilePointer() // 獲取當前的指針位置
void seek(long pos) // 移動當前的指針位置
int skipBytes(int n) // 跳過n個字節