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)://每次读多少字节数组

猜你喜欢

转载自www.cnblogs.com/hatcher-h/p/12897581.html