FileOutputStream
文件輸出流
方法代碼詳解:
public class Demo01 {
public static void main(String[] args) {
// fun1();
// fun2();
/*
* 異常處理
* io 發生異常 都需要停止程序 修改代碼
*/
File file = new File("/Users/james/Desktop/level/123.txt");
// 增加作用域
FileOutputStream oStream = null;
try {
// 創建一個流
// 只要手動創建的流 就需要手動關閉
oStream = new FileOutputStream(file);
oStream.write("Hello".getBytes());
} catch (FileNotFoundException e) {
// 拋出運行時異常(直接停止程序)
throw new RuntimeException("文件找不到");
} catch (IOException e) {
// 文件編寫失敗
throw new RuntimeException("文件無法編寫");
}finally {
try {
// 非空判斷
// 如果是空的 說明 流創建失敗 就不需要關閉
if (oStream != null) {
oStream.close();
}
} catch (IOException e) {
// 關閉資源失敗 停止程序
throw new RuntimeException("流無法關閉");
}
}
}
/**
* @throws FileNotFoundException
* @throws IOException
* 文件的續寫 和 換行
*/
public static void fun2() throws FileNotFoundException, IOException {
File file = new File("/Users/james/Desktop/level/123.txt");
FileOutputStream oStream = new FileOutputStream(file, true);
oStream.write("Hello\n".getBytes());
oStream.close();
}
/**
* @throws FileNotFoundException
* @throws IOException
*/
public static void fun1() throws FileNotFoundException, IOException {
/*
* 構造方法需要文件(被寫入的文件) 和 文件的路徑(被寫入的路徑)
* 寫入文件流程
* 1. 綁定要寫入的文件 或 文件路徑
* 2. 使用write方法 直接寫入
* 3. 關閉資源
*
*/
// 綁定數據的目的地(綁定要寫入的文件)
// 該路徑下 沒有該文件 會幫你創建出這個文件
// 如果有該文件 文件會被覆蓋
File file = new File("/Users/james/Desktop/level/123.txt");
FileOutputStream oStream = new FileOutputStream(file);
// 按ascii 表的值輸入的
oStream.write(97);
byte[] b = {90,100,110};
oStream.write(b);
oStream.write(b, 1, 2);
// 簡單寫法
oStream.write("hello".getBytes());
oStream.close();
}
}
FileInputStream
從文件系統中的某個文件中獲得輸入字節
方法代碼示例:
/*
* 字節輸入流
* inputStream 所有輸入流的父類
* 注意: 字節流 寫入的時候 是一個字節一個字節的寫 讀取是一個字節一個字節的讀
*
* 讀取文件流程
* 1.綁定數據源文件(要讀哪個文件)
* 2.使用read方法讀
* 3.關閉資源
*/
public class Demo01 {
public static void main(String[] args) throws IOException {
File file = new File("/Users/james/Desktop/level/134.txt");
FileInputStream iStream = new FileInputStream(file);
// fun1(iStream);
// fun2(iStream);
int length = (int) file.length();
byte[] bs = new byte[length];
while ((iStream.read(bs)) != -1) {
for (byte b : bs) {
System.out.println(b);
}
}
iStream.close();
}
/**
* @param iStream
* @throws IOException
*/
public static void fun2(FileInputStream iStream) throws IOException {
// 循環讀取
// read 方法 每調用一次就 讀取一個字節
// 循環的時候 read方法只能出現一次
// 聲明一個變量保存 讀出來的結果
int read = 0;
while ((read = iStream.read())!= -1 ) {
System.out.println(read);
}
}
}
輸入輸出流例題應用:
/*
* 使用字節的輸入輸出流進行文件的複製
* (字節流不但可以寫文本 還可以寫圖片 音頻 視頻)
*/
public class Demo01 {
public static void main(String[] args) {
// 獲取系統時間
long start = System.currentTimeMillis();
// 異常處理 複製文件
// 讀
FileInputStream fis = null;
// 寫
FileOutputStream fos = null;
try {
// 複製圖片
fis = new FileInputStream("/Users/james/Desktop/level/圖1.jpg");
fos = new FileOutputStream("/Users/james/Desktop/圖2.jpg");
// 利用緩衝數組讀寫 效率較高!
// 開始讀 讀一個字節 寫一個字節
// int len = 0;
// while ((len = fis.read()) != -1) {
// // 直接把讀出來的寫進文件
// fos.write(len);
// }
// 用字節數組方式讀寫
byte[] bs = new byte[1024];
int len = 0;
while ((len = fis.read(bs)) != -1) {
fos.write(bs, 0, len);
}
} catch (FileNotFoundException e) {
throw new RuntimeException("文件找不到!");
} catch (IOException e) {
throw new RuntimeException("文件複製失敗");
}finally {
// 這時讀寫都完事 可以不用順序關閉
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
throw new RuntimeException("關閉資源失敗");
}finally {
// 寫在一起 如果第一個報異常 第二個流 無法關閉
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
throw new RuntimeException("關閉資源失敗");
}
}
}
long stop = System.currentTimeMillis();
System.out.println(stop - start);
}
}
複製一個文件夾裏的所有txt到另一個文件內
public class Demo03 {
public static void main(String[] args) throws IOException {
File oldfile = new File("/Users/james/Desktop/level");
File newfile = new File("/Users/james/Desktop/xTest");
copyFileTxt(oldfile, newfile);
}
public static void copyFileTxt(File src, File dest) throws IOException {
File[] listFiles = src.listFiles(new FileTxt());
for (File file : listFiles) {
if (file.isFile()) {
// 讀寫
FileInputStream fis = new FileInputStream(file);
File tempFile = new File(dest, file.getName());
FileOutputStream fos = new FileOutputStream(tempFile);
int len = 0;
byte[] b = new byte[1024];
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
fis.close();
fos.close();
}else {
copyFileTxt(file, dest);
}
}
}
}
class FileTxt implements FileFilter{
@Override
public boolean accept(File pathname) {
// 文件不被過濾
if (pathname.isDirectory()) {
return true;
}
// 過濾不是txt的
String name = pathname.getName();
return name.endsWith("txt");
}
}