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
//該操做實現類三步
/*
一、建立a.txt
二、建立file對象
三、將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

### 異常處理
```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
//該操做實現類三步
/*
一、建立a.txt
二、建立file對象
三、將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)://每次讀多少字節數組
相關文章
相關標籤/搜索