JAVA異常以及位元組流

異常

JAVA異常可以分為編譯時候出現的異常和執行時候出現的異常
JVM默認處理異常的方法是拋出異常

異常處理

//第一種
try{
可能會出錯的程式碼
}catch{
發生異常後處置方法
}finally{
處理完畢後需要執行的程式碼
}
//第二種
throws 異常類
thorows作用:當前方法不知道如何處理異常,就可以用throws,誰調用該方法,誰處理異常。

IO流

存在數據交互的地方就存在IO

File類

構造方法
//構造方法
File file = new File(String file);
File file = new File(String parent,String child);
File file = new File(File parent, String child);

創建文件/文件夾
//文件存在返回flase
boolean file.createFile("a.txt");
//創建一個文件夾
file.mkdir("/c");
//遞歸創建文件夾
file.mkdir("/c/a");
刪除文件、文件夾
//不可直接刪除包含子結構的文件夾
file.delete();
其他操作
file.isFile();
file。isDirectory();
file.exists();
file.getAbsolutePath()://絕對路徑
file.getPath();
file.getName();

OutputStream

輸出位元組流的超類
FileOutputStream
//該操作實現類三步
/*
1、創建a.txt
2、創建file對象
3、將a.txt指向file
*/
FileOutputStream file = new FileOutputStream ("a.txt");
//寫操作的三種方法
public void write(int b):一次寫一個位元組
public void write(byte[] b):一次寫一個位元組數組
public void write(byte[] b,int off,int len):一次寫一個位元組數組的一部分

FileInputStream

public int read(byte[] b)://每次讀多少位元組數組

```## 異常

JAVA異常可以分為編譯時候出現的異常和執行時候出現的異常
JVM默認處理異常的方法是拋出異常

### 異常處理
```java
//第一種
try{
可能會出錯的程式碼
}catch{
發生異常後處置方法
}finally{
處理完畢後需要執行的程式碼
}
//第二種
throws 異常類
thorows作用:當前方法不知道如何處理異常,就可以用throws,誰調用該方法,誰處理異常。

IO流

存在數據交互的地方就存在IO

File類

構造方法
//構造方法
File file = new File(String file);
File file = new File(String parent,String child);
File file = new File(File parent, String child);

創建文件/文件夾
//文件存在返回flase
boolean file.createFile("a.txt");
//創建一個文件夾
file.mkdir("/c");
//遞歸創建文件夾
file.mkdir("/c/a");
刪除文件、文件夾
//不可直接刪除包含子結構的文件夾
file.delete();
其他操作
file.isFile();
file。isDirectory();
file.exists();
file.getAbsolutePath()://絕對路徑
file.getPath();
file.getName();

OutputStream

輸出位元組流的超類
FileOutputStream
//該操作實現類三步
/*
1、創建a.txt
2、創建file對象
3、將a.txt指向file
*/
FileOutputStream file = new FileOutputStream ("a.txt");
//寫操作的三種方法
public void write(int b):一次寫一個位元組
public void write(byte[] b):一次寫一個位元組數組
public void write(byte[] b,int off,int len):一次寫一個位元組數組的一部分

FileInputStream

public int read(byte[] b)://每次讀多少位元組數組

Tags: