1.文件
文件在程序中是以流的形式操作的,流是指数据在数据源和程序之间经历的路径
输入流:数据源到内存
输出流:内存到数据源
2.常用文件操作
创建文件对象相关构造器和方法
//1.根据路径构建一个File对象new File(String pathname);//2.根据父目录文件+子路径构建new File(File parent, String child);//3.根据父目录+子路径构建new File(String parent, String child);//4.创建新文件createNewFile();//5.获取文件名字getName();...getAbsolutePath();getParent();length();//字节长度//创建目录mkdir();//创建多级目录mkdirs();
3.IO流原理和流的分类
流的分类:
1、按操作数据单位的不同分为:字节流(按字节)、字符流(按字符);
2、按数据流的流向:输入、输出流;
3、流的角色不同:节点流、处理流(包装流);
抽象基类:
| 抽象基类 | 字节流 | 字符流 |
|---|---|---|
| 输入流 | InputStream | Reader |
| 输出流 | OutputStream | Writer |
所有的IO流类都是由四个类派生出来的
4.FileInputStream和FileOutputStream
public class FileInputStream_ {@Testpublic void read() {FileInputStream fileInputStream = null;//为了提高效率,可以将空间扩大byte[8 * 1024]byte[] buf = new byte[8];//readLen为读到的长度,以字节为单位int readLen = 0;try {fileInputStream = new FileInputStream("src\\my.txt");//1、read()返回该字节的码值//2、read(byte[] b)返回读到的字节数组长度while ((readLen = fileInputStream.read(buf)) != -1) {//new String(byte[], int off, int length)构造字符串System.out.print(new String(buf,0,read));}} catch (IOException e) {e.printStackTrace();} finally {try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}}}
FileOutputStream
public class FileOutputStream_ {@Testpublic void writeFile() {String path = "src\\a.txt";FileOutputStream fileOutputStream = null;byte[] buf = null;try {//new FileOutputStream(path)是覆盖形式的,会覆盖//new FileOutputStream(path,true)追加形式fileOutputStream = new FileOutputStream(path,true);String str = "hello,world,mysql";buf = str.getBytes();//getBytes()可以将字符串转换成字节数组fileOutputStream.write(buf, 0, 3);//write(buf[],int off,int length),将length字节从偏移量off的指定数组写入此文件输出流} catch (IOException e) {e.printStackTrace();} finally {try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}}
5.FileReader和FileWriter
FileReader和FileWriter是字符流,按照字符流来操作IO
FileReader相关API:
//1.new FileReader(File/String);//2.每次读取单个字符,并返回字符read();//3.批量读取多个字符到数组,返回读取到的字符数read(char[] b);//4.将char[]转换成Stringnew String(char[]);//5.将char[]的指定部分转换成Stringnew String(char[], int off, int length)
FileWriter相关API:
//1.覆盖new FileWriter(File/String);//2.追加new FileWriter(File/String, true);//3.写入单个字符write();//4.写入指定数组write(char[]);//5.写入指定数组的指定部分write(char[], int off, int length);//6.写入整个字符串write(String);//7.写入字符串的指定部分write(String, int off, int length);//8.String类,将String转换成char[]toCharArray();
FileWriter使用后,必须要关闭或者刷新,否则写入不到指定的文件
6.包装流
处理流也叫包装流,是连接已存在的流(节点流或处理流)之上,为程序提供更为强大的读写功能,如BufferedReader、BufferedWriter.
public class BufferedReader extends Reader {private Read in;}
BufferedReader类中,有一个属性Reader,即可以封装一个节点流,该节点流是任意的,只要是Reader子类
节点流和处理流的区别和联系:
1、节点流是底层流、直接跟数据源联系;
2、处理流包装节点流,既可以消除不同节点流的实现差异,也可以提供更方便的方法来完成输入输出;
3、处理流对节点流完成包装,使用了修饰器设计模式,不会直接与数据源相连;
处理流的功能主要体现在以下两个方面:
1、性能的提高:主要以增强缓冲的方式来提高输入输出的效率;
2、操作的便捷:处理流可能提供了一系列便捷的方法来一次输入输出的大批量数据,使用更加方便灵活。
7.BufferedReader和BufferedWriter
public class BufferedWriter_ {@Testpublic void writeFile() {String path = "src\\c.txt";BufferedWriter bufferedWriter = null;try {bufferedWriter = new BufferedWriter(new FileWriter(path, true));bufferedWriter.write("how do you do?");bufferedWriter.newLine();bufferedWriter.write("i am fine thanks");bufferedWriter.newLine();//在使用时,关闭外层流即可bufferedWriter.close();} catch (IOException e) {e.printStackTrace();}}}
8.BufferedInputStream和BufferedOutputStream
public class BufferedInputStream_ {@Testpublic void copyFile() {String srcFile = "F:\\张宇考研基础班30讲\\mymovie.mp4";String desFile = "F:\\张宇考研基础班30讲\\mymovie_copy1.mp4";BufferedInputStream bis = null;BufferedOutputStream bos = null;try {bis = new BufferedInputStream(new FileInputStream(srcFile));bos = new BufferedOutputStream(new FileOutputStream(desFile));byte[] bytes = new byte[8 * 1024];int readLen = 0;while ((readLen = bis.read(bytes)) != -1) {bos.write(bytes, 0, readLen);}bis.close();bos.close();} catch (IOException e) {e.printStackTrace();}}}
9.对象流-ObjectInputStream和ObjectOutputStream
序列化和反序列化
1、序列化就是保存数据时,保存数据的值和数据类型;
2、反序列化就是在恢复数据时,恢复数据的值和数据类型;
3、需要让某个对象支持序列化时,则必须让其类是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一:
》Serializable
》Externalizable
注意事项:
1、读写顺序要一致;
2、要求实现序列化或反序列话对象,需要实现Serializable;
3、序列化的类中建议添加SerialVersionUID,为了提高版本的兼容性;
4、序列化时,默认将里面所有属性进行序列化,但除了static或transient修饰的成员;
5、序列化对象时,要求里面属性的类型也要实现序列化接口;
6、序列化具备可继承性,也就是如果某个类已经实现了序列化,则它的所有子类已经默认实现了序列化;
10.标准输入输出流
| 类型 | 默认设备 | |
|---|---|---|
| System.in 标准输入 | InputStream | 键盘 |
| System.out 标准输出 | PrintStream | 显示器 |
