IO流
一,异常
1,异常分类:
异常分为Error和Exception,Exception又分为RuntimeException(运行期异常,我们需要修正代码)和编译期异常(必须处理的,否则程序编译不通过)。
2,异常处理:
A:JVM的默认处理:把异常的名称,原因,位置等信息输出在控制台,但是呢程序不能继续执行了。
B:自行处理:try...catch...finally:自己编写处理代码,后面的程序可以继续执行。
throws: 把处理不了的,在方法上声明,告诉调用者,这里有问题。
C:throw和throws是的区别
throw:在方法体中,后面跟的是异常对象名,并且只能是一个 throw抛出的是一个异常对象,说明这里肯定有一个异常产生了
throws:在方法声明上,后面跟的是异常的类名,可以是多个throws是声明方法有异常,是一种可能性,这个异常并不一定会产生
3,异常处理的变形
try...catch...finally
try...catch...
try...catch...catch...
try...catch...catch...fianlly
try...finally
注意:try...catch...catch...fianlly方式必须是子类 异常或者平级异常在前面,父类异常在后面。使用catch(异常一|异常二 变量)方式,需要两个异常必须是平级。
4,自定义异常
两种方式:继承Exception或者继承RuntimeException,只需要提供无参构造和一个带参构造即可。
public class MyException extends Exception {
public MyException() {
}
public MyException(String message) {
super(message);
}
}
public class Teacher {
public void check(int score) throws MyException {
if (score > 100 || score < 0) {
throw new MyException("分数必须在0-100之间");
} else {
System.out.println("分数没有问题");
}
}
}
public class StudentDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生成绩:");
int score = sc.nextInt();
Teacher t = new Teacher();
try {
t.check(score);
} catch (MyException e) {
e.printStackTrace();
}
}
}
5,异常处理注意事项
A:父的方法有异常抛出,子的重写方法在抛出异常的时候必须要小于等于父的异常
B:父的方法没有异常抛出,子的重写方法不能有异常抛出
C:父的方法抛出多个异常,子的重写方法必须比父少或者小
二:File文件操作
1,File类概述
A:flie类是文件和目录(文件夹)路径名的抽象表示形式
B:构造方法:
File(String pathname):根据一个路径得到File对象
File(String parent, String child):根据一个目录和一个子文件/目录得到File对象
File(File parent, String child):根据一个父File对象和一个子文件/目录得到File对象
2,Flie类的方法
A:创建功能:
public boolean createNewFile():创建文件 如果存在这样的文件,就不创建了
public boolean mkdir():创建文件夹 如果存在这样的文件夹,就不创建了
public boolean mkdirs():创建文件夹,如果父文件夹不存在,会帮你创建出来
B:删除功能:public boolean delete()
注意:如果你创建文件或者文件夹忘了写盘符路径,那么,默认在项目路径下。Java中的删除不走回收站。要删除一个文件夹,请注意该文件夹内不能包含文件或者文件夹
C:重命名功能:public boolean renameTo(File dest),如果路径名相同,就是改名。 如果路径名不同,就是改名并剪切。
D:判断功能:
public boolean isDirectory():判断是否是目录
public boolean isFile():判断是否是文件
public boolean exists():判断是否存在
public boolean canRead():判断是否可读
public boolean canWrite():判断是否可写
public boolean isHidden():判断是否隐藏
E:获取功能:
public String getAbsolutePath():获取绝对路径
public String getPath():获取相对路径
public String getName():获取名称
public long length():获取长度。字节数
public long lastModified():获取最后一次的修改时间,毫秒值
F: 获取功能(数组):
public String[] list():获取指定目录下的所有文件或者文件夹的名称数组
public File[] listFiles():获取指定目录下的所有文件或者文件夹的File数组
三:IO框架
1,IO流的分类
按功能分为输出流和输入流
按类型分为字节流和字符流
2,字节输出流-FileOutputStream
A,构造方法
FileOutputStream(File file) 创建文件输出流以写入由指定的 File对象表示的文件。
FileOutputStream(File file, boolean append) 创建文件输出流以写入由指定的 File对象表示的文件。
FileOutputStream(FileDescriptor fdObj) 创建文件输出流以写入指定的文件描述符,表示与文件系统中实际文件的现有连接。
FileOutputStream(String name) 创建文件输出流以指定的名称写入文件。
FileOutputStream(String name, boolean append) 创建文件输出流以指定的名称写入文件。
B:主要方法
void close() :关闭此文件输出流并释放与此流相关联的任何系统资源。
protected void finalize() :清理与文件的连接,并确保当没有更多的引用此流时,将调用此文件输出流的 close方法。
FileChannel getChannel() :返回与此文件输出流相关联的唯一的FileChannel对象。
FileDescriptor getFD() :返回与此流相关联的文件描述符。
void write(byte[] b) :将 b.length个字节从指定的字节数组写入此文件输出流。
void write(byte[] b, int off, int len) :将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。
void write(int b) :将指定的字节写入此文件输出流。
3,字节输出流-FileInputStream
A:构造方法
FileInputStream(File file) :通过打开与实际文件的连接创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。
FileInputStream(FileDescriptor fdObj) :创建 FileInputStream通过使用文件描述符 fdObj ,其表示在文件系统中的现有连接到一个实际的文件。
FileInputStream(String name) :通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
B:主要方法
void close() :关闭此文件输入流并释放与流相关联的任何系统资源。
protected void finalize() :确保当这个文件输入流的 close方法没有更多的引用时被调用。
FileChannel getChannel() :返回与此文件输入流相关联的唯一的FileChannel对象。
FileDescriptor getFD() :返回表示与此 FileInputStream正在使用的文件系统中实际文件的连接的 FileDescriptor对象。
int read() :从该输入流读取一个字节的数据。
int read(byte[] b) :从该输入流读取最多 b.length个字节的数据为字节数组。
int read(byte[] b, int off, int len) :从该输入流读取最多 len字节的数据为字节数组。
4,使用字节流实现文件复制
A:一次读取一个字节
public class CopyFileDemo2 {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("c:\\a.txt");
// 封装目的地
FileOutputStream fos = new FileOutputStream("d:\\b.txt");
// 复制数据
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// 释放资源
fos.close();
fis.close();
}
}
B:一次读取一个数组
public class CopyMp4Demo {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("e:\\哥有老婆.mp4");
// 封装目的地
FileOutputStream fos = new FileOutputStream("copy.mp4");
// 复制数据
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
// 释放资源
fos.close();
fis.close();
}
}
五:缓冲流
1,BufferedInputStream
BufferedInputStream(InputStream in) :创建一个 BufferedInputStream并保存其参数,输入流 in ,供以后使用。
BufferedInputStream(InputStream in, int size) :创建 BufferedInputStream具有指定缓冲区大小,并保存其参数,输入流 in ,供以后使用。
2,BufferedOutputStream
BufferedOutputStream(OutputStream out) :创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
BufferedOutputStream(OutputStream out, int size) :创建一个新的缓冲输出流,以便以指定的缓冲区大小将数据写入指定的底层输出流。
3,使用
public class CopyMp4Demo {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
method4("e:\\哥有老婆.mp4", "copy4.mp4");
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
}
// 高效字节流一次读写一个字节数组:
public static void method4(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
// 高效字节流一次读写一个字节:
public static void method3(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}}

浙公网安备 33010602011771号