代碼
java IO 操作分析
package jdk.util.sourceCode;
import java.io.*;
/**
* 經常會遇到各種 IO 流操作,IO 流操作一般分爲兩類:字符流和字節流。
* 以 "Reader" 結尾都是字符流,操作的都是字符型的數據
* 以 "Stream" 結尾的都是字節流,操作的都是 byte 類型的數據
* 二者的區別:
* 字節流沒有緩衝區,是直接輸出的;而字符流是先輸出到緩衝區,然後在調用 close() 方法後再輸出信息
* 處理對象不同,字節流能處理所有類型的數據,但是字符流只能處理字符類型的數據(只要是處理純文本數據,就優先考慮使用字符流,除此之外都使用字節流)
* java byte -> short -> int -> long 1byte -> 2byte -> 4byte -> 8byte
*
*
*
* InputStream 和 OutputStream 是各種輸入輸出字節流的基類,所有字節流都繼承於這兩個基類
*
*
* FileInputStream 和 FileOutputStream 這兩個從字面意思很容易理解,是對文件的字節流操作,也是最常見的 IO 操作流
*
*
* 非流式文件類 -- File 類
* 從定義來看,File 類是 Object 的直接子類,同時它繼承了 Comparable 接口可以進行數組的排序
* File 類的操作包括文件的創建,刪除,重命名,得到文件/文件夾的路徑,創建時間等
* File 類是對文件系統中文件以及文件夾進行封裝的一個對象,可以通過對象的思想來操作文件和文件夾
*
/
/**
* @author: [email protected]
* @date: 2019/5/25 15:40
* @description:
* @version: 1.0
* @className: TestIO
*/
public class TestIO {
public static void main(String[] args){
// 1.調用 新建文件
// createFile("F:\\github\\[email protected]\\jdk\\src\\main\\java\\jdk\\util\\sourceCode\\", "測試io.txt");
// 2.調用刪除文件
// deleteFile("F:\\github\\[email protected]\\jdk\\src\\main\\java\\jdk\\util\\sourceCode\\","測試io.txt");
// 3.調用創建文件夾
// createFolder("F:\\github\\[email protected]\\jdk\\src\\main\\java\\jdk\\util\\sourceCode\\", "測試io文件夾");
// 4.列出指定目錄下面的所有文件,包括隱藏文件
// listFiles("F:\\github\\[email protected]\\jdk\\src\\main\\java\\jdk\\util\\sourceCode\\");
// 5.判斷指定的 文件夾是否是一個 目錄(即是否是一個 文件夾)
// isFolder("F:\\\\github\\\\[email protected]\\\\jdk\\\\src\\\\main\\\\java\\\\jdk\\\\util\\\\sourceCode\\", "測試io文件夾");
// 6. 向指定的文件中(需要在文件名中給出路徑和文件名,我這裏是爲了簡便這樣寫了)通過 字節流 寫入數據 (這裏前提:認爲該文件已經存在,不需要再創建)
// writeFileByByte("F:\\github\\[email protected]\\jdk\\src\\main\\java\\jdk\\util\\sourceCode\\測試io.txt");
// 7.從指定的文件中讀取內容
// readFileByByte("F:\\github\\[email protected]\\jdk\\src\\main\\java\\jdk\\util\\sourceCode\\測試io.txt");
// 8. 從 指定文件讀取內容並寫入到 目標文件
readWriteFile("F:\\game\\xx.mp4",
"E:\\github-project\\jdk\\src\\main\\java\\jdk\\util\\sourceCode\\測試io.txt");
}
/**
* 因爲 io 流基本是與 File(文件/文件夾) 操作密不可分的,因此 io 的操作,只要涉及到文件,文件夾的都必須使用 File 類
* 在指定的路徑下,新建一個 指定文件名的 文件
* @param path 文件路徑
* @param fileName 文件名
*/
public static void createFile(String path, String fileName){
// 因爲是在 操作 文件,所以用到 File 對象【記住:所有與文件/文件夾操作相關的內容,都必須第一時間想到要用 File 對象】
File file = new File(path+fileName); // 實例化一個 file 操作對象
try {
file.createNewFile(); // 調用 file 文件/文件夾 實例對象的 方法,來新建文件
System.out.println("目標文件已存在: " + path + fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 刪除一個指定路徑下的 文件
* @param path 該文件的路徑
* @param fileName 該文件的文件名
*/
public static void deleteFile(String path, String fileName){
File file = new File(path+fileName);
if(file.exists()){
file.delete();
System.out.println("目標文件已刪除");
}else{
System.out.println("要刪除的目標文件不存在");
}
}
/**
* 新建一個 文件夾
* @param path 路徑
* @param folderName 文件夾名
*/
public static void createFolder(String path, String folderName){
File file = new File(path+folderName);
file.mkdir();
System.out.println("該文件夾已經存在於: " + path + folderName);
}
/**
* 列出指定目錄下面的所有文件
* @param path 目錄的路徑名
*/
public static void listFiles(String path){
File file = new File(path);
if (file.isDirectory()){
File[] fileArray = file.listFiles();
for (int i = 0; i < fileArray.length; i++){
System.out.println( "該目錄下的文件: " + fileArray[i]);
System.out.println( "該目錄下的文件或文件夾的名字: " + fileArray[i].getName());
}
}else{
System.out.println(path + " 目錄不存在");
}
}
/**
* 判斷給定的 文件夾 是否是一個目錄
* @param path
*/
public static void isFolder(String path, String folderName){
File file = new File(path + folderName);
if (file.isDirectory()){
System.out.println(path + folderName + " 是一個目錄");
}else{
System.out.println(path + folderName + " 不是一個目錄");
}
}
/**
* 通過 字節流 向 指定文件 寫入內容
* @param fileName 文件名,這裏爲了簡化,文件名中帶上 路徑
*/
public static void writeFileByByte(String fileName){
File file = new File(fileName);
OutputStream outputStream = null; // 從內存中 寫入內容 到 文件中,這是輸出流,因此要用 輸出流
// FileOutputStream 的構造器大體上有兩類:一類是 傳入一個帶有文件名和文件路徑的字符串;另一類是 傳入一個 File 文件/文件夾對象
try {
outputStream = new FileOutputStream(file, true); // 給 file 文件對象 構造一個字節輸出流
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 這裏穿插一個小知識點,即我們 給一個 int 參數,但是我們要讓 outputStream 以 byte[] 的形式寫入,接下來就看 int 轉 byte[] 吧
int a = 12345678; // 爲什麼這樣呢?因爲 一個 int 是 4個byte,所以一個 int 轉成 byte[] 後,一定是裏面包含4個byte元素的 byte[] 數組
byte[] b = new byte[]{
(byte) ((a >> 24) & 0xFF),
(byte) ((a >> 16) & 0xFF),
(byte) ((a >> 8) & 0xFF),
(byte) ((a ) & 0xFF)
};
try {
outputStream.write(b); // 這裏還有一個問題沒解決:寫入的時候,選擇編碼格式(稍後解決)
outputStream.close();
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* 通過 字節流 從 指定文件 讀取輸出內容
* @param fileName 文件名,這裏爲了簡化,文件名中帶上 路徑
*/
public static void readFileByByte(String fileName){
File file = new File(fileName);
InputStream inputStream = null; // 從 硬盤中 讀取內容 到 內存中,這是 輸入流,因此聲明 輸入流 對象
try {
inputStream = new FileInputStream(file);
// inputStream 讀取內容有5個方法 read():默認讀取一個byte,readBytes(byte b[], int off, int len)
// 這裏我們採用 read(byte b[], int off, int len) 方法
byte[] byter = new byte[1024]; // 所以先實例化一個 byte[]
int len = inputStream.read(byter);
inputStream.close();
// 最後我們輸出一下讀取到的內容
System.out.println(new String(byter, 0, len));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @author: [email protected]
* @param: [sourceFile, desFile]
* @return: void
* @date: 2019/5/25 18:04
* @version: 1.0
* @description: 最後來一個 從 指定文件中 讀取內容 到 指定目標文件中
*/
public static void readWriteFile(String sourceFile, String desFile){
File inputFile = new File(sourceFile);
File outputFile = new File(desFile);
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(inputFile);
byte[] byter = new byte[1024];
inputStream.read(byter);
outputStream = new FileOutputStream(outputFile, true);
outputStream.write(byter);
outputStream.close();
inputStream.close();
System.out.println("操作完成");
}catch (Exception e){
System.out.println(e.getMessage());
}
}
}
聲明
原創手敲,by 拉丁小毛