第16章 位元組流與字元流
72 流的基本概念
File類是唯一一個與檔案本身有關的程式處理類
File類隻能夠操作檔案本身,而不能操作檔案内容
IO操作:輸入輸出操作
java.io 抽象類
輸出 輸入
位元組流:OutputStream, InputStream
字元流:Writer, Reader
檔案處理流程:
1、File找到一個檔案
2、通過位元組流或字元流的子類為父類對象執行個體化
3、利用位元組流或字元流中的方法實作資料出入與輸出操作
4、流的操作屬于資源操作,資源操作必須進行關閉
73 OutputStream位元組輸出流
實作代碼
public interface AutoCloseable {
void close() throws Exception;
}
public interface Closeable extends AutoCloseable {
public void close() throws IOException;
}
public interface Flushable {
void flush() throws IOException;
}
public abstract class OutputStream
implements Closeable, Flushable{
public abstract void write(int b) throws IOException;
public void write(byte b[]) throws IOException;
public void write(byte b[], int off, int len) throws IOException;
}
// 子類
public class FileOutputStream extends OutputStream{
// 覆寫
public FileOutputStream(File file) throws FileNotFoundException
// 追加
public FileOutputStream(File file, boolean append)
throws FileNotFoundException;
}
内容輸出到檔案
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
class Demo{
public static void main(String[] args) throws IOException {
File file = new File("demo/demo.txt");
// 父級目錄不存在則建立
if(!file.getParentFile().exists()){
file.getParentFile().mkdir();
}
String message = "這是輸出的内容";
// 将字元串轉換為位元組數組輸出到檔案,并關閉檔案
FileOutputStream output = new FileOutputStream(file);
output.write(message.getBytes());
output.close();
}
}
自動關閉的寫法
try(FileOutputStream output = new FileOutputStream(file)){
output.write(message.getBytes());
}catch (IOException e){
e.printStackTrace();
}
使用追加換行輸出
String message = "這是輸出的内容\r\n";
FileOutputStream output = new FileOutputStream(file, true);
output.write(message.getBytes());
output.close();
74 InputStream位元組輸入流
public abstract class InputStream implements Closeable{
// 讀取單個位元組資料,讀到檔案底部傳回-1
public abstract int read() throws IOException;
// 讀取一組位元組資料,傳回讀取的個數,檔案底部傳回-1
public int read(byte b[]) throws IOException;
public int read(byte b[], int off, int len) throws IOException;
}
檔案尾部傳回 -1, 表示檔案讀取完成
子類
public class FileInputStream extends InputStream{
public FileInputStream(String name) throws FileNotFoundException
public FileInputStream(File file) throws FileNotFoundException
}
讀取示例
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
class Demo{
public static void main(String[] args) throws IOException {
File file = new File("demo/demo.txt");
FileInputStream input = new FileInputStream(file);
// 開辟緩沖區讀取資料
byte[] data = new byte[1024];
int len = input.read(data);
System.out.println("[" + new String(data, 0, len) + "]");
input.close();
}
}
75 Writer字元輸出流
Writer可以直接輸出字元串
public abstract class Writer
implements Appendable, Closeable, Flushable{
public void write(char cbuf[]) throws IOException;
public void write(String str) throws IOException;
}
public class OutputStreamWriter extends Writer
public class FileWriter extends OutputStreamWriter{
public FileWriter(String fileName, boolean append);
public FileWriter(String fileName)
public FileWriter(File file)
public FileWriter(File file, boolean append)
}
代碼執行個體
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
class Demo{
public static void main(String[] args) throws IOException {
File file = new File("demo/demo.txt");
FileWriter writer = new FileWriter(file);
writer.write("hello java");
writer.append("你好!java!");
writer.close();
}
}
76 Reader字元輸入流
繼承關系
public abstract class Reader implements Readable, Closeable
public class InputStreamReader extends Reader
public class FileReader extends InputStreamReader{
public FileReader(File file);
public FileReader(String fileName);
}
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
class Demo{
public static void main(String[] args) throws IOException {
File file = new File("demo/demo.txt");
FileReader reader = new FileReader(file);
char[] data= new char[1024];
int len = reader.read(data);
System.out.println(new String(data, 0, len));
reader.close();
}
}
字元流讀取隻能按照數組資料讀取
77 位元組流與字元流的差別
不使用close關閉
使用位元組流輸出 OutputStream 正常輸出
使用字元流輸出 Writer 無法輸出,使用了緩沖區
close會強制重新整理緩沖區(flush)
位元組流不使用緩沖區,字元流使用緩沖區
78 轉換流
位元組流與字元流操作的功能轉換
import java.io.*;
class Demo{
public static void main(String[] args) throws IOException {
File file = new File("demo/demo.txt");
// 位元組流轉字元流操作
OutputStream out = new FileOutputStream(file);
Writer wirter = new OutputStreamWriter(out);
wirter.write("你好");
wirter.close();
}
}
OutputStream(Closeable, Flushable)
-FileOutputStream
InputStream(Closeable)
-FileInputStream
Writer(Appendable, Closeable, Flushable)
-OutputStreamWriter
-FileWriter
Reader(Readable, Closeable)
-InputStreamReader
-FileReader
緩存,程式中間緩沖區
位元組資料:101010101…
79 綜合實戰:檔案拷貝
實作檔案拷貝操作
使用位元組流
方案一:
全部讀取,一次性輸出
方法二:
每次讀取一部分,輸出一部分
import java.io.*;
class FileUtil {
public static void copyFile(String src, String target) throws IOException {
InputStream input = new FileInputStream(src);
OutputStream output = new FileOutputStream(target);
byte[] data = new byte[1024];
int len = 0;
while ((len = input.read(data)) != -1) {
output.write(data, 0, len);
}
input.close();
output.close();
}
public static void copyDir(String src, String target) throws IOException {
File srcFile = new File(src);
File targetFile = new File(target);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
File[] results = srcFile.listFiles();
if (results != null) {
for (File file : results) {
String fileName = targetFile + File.separator + file.getName();
if (file.isDirectory()) {
copyDir(file.getPath(), fileName);
} else {
copyFile(file.getPath(), fileName);
}
}
}
}
}
class Demo {
public static void main(String[] args) throws IOException {
FileUtil.copyDir("demo", "demo2");
System.out.println("拷貝完成");
}
}
如果拷貝目錄則使用遞歸拷貝