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)://每次读多少字节数组
相关文章
相关标签/搜索