1, io流: 实现文件数据的读写,文件的拷贝,上传下载.. 流: 一连串流动的数据,以先入先出的方式进行传输 流就是管道
2,数据源 --IO-->目的地 分类: 流向分: (一切以程序为中心) 输入流 输出流 操作单元: 字节流 :万能流 * 字符流 :只能操作纯文本内容.. 功能分: 节点流 : 真实做读写.. 功能流(处理流) : 增强节点流功能与性能 字节流: 节点流: 字节数组流 ByteArray 文件流 File *** 字节流的输入流节点流 InputStream 字节输入流的父类 FileInputStream 文件字节输入流 字节流的输出流节点流 OutputStream
public class Class01_Io { public static void main(String[] args) throws IOException { //创建文件字节输入流 InputStream is = new FileInputStream("D://haha.txt"); //读入 //int read() 从输入流中读取下一个数据字节。 读到数据返回数据字节数 ,没读到数据返回-1 int num = is.read(); //3.处理数据 System.out.println((char) num); System.out.println((char) is.read()); System.out.println((char) is.read()); System.out.println((char) is.read()); System.out.println((char) is.read()); System.out.println((char) is.read()); System.out.println((char) is.read()); System.out.println(is.read()); //每次读入一个字节,通过循环重复读入,重复处理 int n = -1; while ((n=is.read())!=-1){ System.out.println((char) n); } //关闭 is.close(); } }
3,每次读入一个字节read(),循环读入-->效率较低 优化: byte[] int read(byte[] b) 从此输入流 b.length最多 b.length字节的数据读 b.length字节数组。 返回读入到数组中数据的个数
public class Class02_IO { public static void main(String[] args) throws IOException { //创建一个字节输入流 InputStream num = new FileInputStream("D://haha.txt"); //读写 int num1 = num.read(); System.out.println((char) num.read()); System.out.println((char) num.read()); System.out.println((char) num.read()); System.out.println((char) num.read()); System.out.println((char) num.read()); int i = -1; byte[] is = new byte[1024]; //处理数据 while ((i=num.read(is))!=-1){ System.out.println(new String(is,0,i)); } //byte[] readAllBytes() 从输入流中读取所有剩余字节。 byte[] arr = num.readAllBytes(); System.out.println(new String(arr)); //关闭流 num.close(); } }
6.5.1字节流
4, OutputStream 字节输出的父类 FileOutputStream 文件字节输出流--> 节点流 注意: 如果目的地文件不存在,系统会自动创建目的地文件,如果文件夹不存在,系统不会创建
public class Class03_Io { public static void main(String[] args) throws IOException { //创建文件字节输出流 //OutputStream on = new FileOutputStream("D://heihei.txt"); //追加 OutputStream on = new FileOutputStream("D://heihei.txt",true); //准备数据 byte[] arr = "上课敲代码".getBytes(); //写出 on.write(arr); //刷出 on.flush(); //关闭 on.close(); } }
6.5.2文件拷贝
1, 文件拷贝: 文件--输入--> 程序 --输出-->文件
步骤: 1.创建流 2.读写 3.刷出 4.关闭 后打开的先关闭
public class Class01_Copyfile { public static void main(String[] args) { //声明 InputStream in = null; OutputStream is = null; //创建流 try { in = new FileInputStream("D://haha1.txt"); is = new FileOutputStream("D://haha2.txt"); //读写 int len = -1; //读入到数组中的个数 byte[] arr = new byte[1024]; while ((len=in.read(arr))!=-1){ is.write(arr,0,len); //刷出 is.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { //关闭 if (is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
6.5.3字符流
1,字符流(只能读写纯文本数据) 文件流 节点流 输入流 Reader FileReader 文件字符输入流 输出流 Writer FileWriter 文件字符输出流
public class Class03_CopyFile { public static void main(String[] args) { Reader rt = null; Writer rn = null; //创建流 try { rt = new FileReader("D://A/haha.txt"); rn = new FileWriter("D://A/haha2.txt"); //读写流 char[] arr = new char[1024]; //记录读取到数据中每个的个数 int len = -1; while ((len=rt.read(arr))!=-1){ rn.write(arr,0,len); //刷出 rn.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { //关闭 if (rn!=null){ try { rn.close(); } catch (IOException e) { e.printStackTrace(); } } if (rt!=null){ try { rt.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
6.5.4功能流
1,功能流(节点流) 缓冲流 : 加快读写效率 字节缓冲流 BufferedInputStream BufferedOutputStream 字符缓冲流
public class Class01_BufferedInputStream { public static void main(String[] args) throws IOException { //创建流 InputStream is = new BufferedInputStream(new FileInputStream("D://haha1.txt")); OutputStream in = new BufferedOutputStream(new FileOutputStream("D://haha3.txt")); //读写 byte[] arr = new byte[1024]; int len = -1; while ((len=is.read(arr))!=-1) { in.write(arr, 0, len); } //刷出 in.flush(); //关闭 in.close(); is.close(); } }
6.5.5字符缓冲流
字符缓冲流 输入 : BufferedReader 新增功能: String readLine() 读一行文字。 输出 : BufferedWriter 新增功能: void newLine() 写一个行分隔符。 注意: 有新增能需要调用,不能多态调用
public class Class01_BufferedReader { public static void main(String[] args) throws IOException { //创建流 BufferedReader rt = new BufferedReader(new FileReader("D://haha1.txt")); BufferedWriter rn = new BufferedWriter(new FileWriter("D://haha4.txt")); //读写流 String arr = null; while ((arr=rt.readLine())!=null) { rn.write(arr); rn.newLine();//换行 } //刷出 rn.flush(); //关闭 rn.close(); rt.close(); } }
6.5.6Data流
1, 字节流的功能流 Data流|基本数据类型 : 读写数据的同时保留数据的数据类型(基本数据类型+String) DataInputStream 新增方法: readXxx() DataOutputStream 新增方法: writeXxx() 注意: 写出与读入的顺序要保持一致 必须从写出的源文件中读入数据 java.io.EOFException
public class Class01_Data { public static void main(String[] args) throws IOException { writeToFile("D://hehe1.txt"); readFromFile("D://hehe1.txt"); } //创建输入流 public static void readFromFile(String path) throws IOException { //定义输入流 DataInputStream rt = new DataInputStream(new FileInputStream(path)); //读取 int i = rt.readInt(); boolean bd = rt.readBoolean(); String str = rt.readUTF(); //处理数据 System.out.println(i); System.out.println(bd); System.out.println(str); //关闭 rt.close(); } //创建输出流 public static void writeToFile(String path) throws IOException { //写 //定义输出流 DataOutputStream is = new DataOutputStream(new FileOutputStream(path)); //准备数据 int i = 10; boolean db = true; String str = "今天很开心"; //写出 is.writeInt(i); is.writeBoolean(db); is.writeUTF(str); //刷出 is.flush(); //关毕 is.close(); } }
6.5.7字节流的功能流
1,字节流的功能流 Object流|对象流 : 数据+数据类型(基本|引用) 序列化: 把对象数据转为可存储或者可传输的状态过程称为序列化 反序列化 序列化输出流 ObjectOutputStream 新增功能: void writeObject(Object obj) 将指定的对象写入ObjectOutputStream。 反序列化输入流 ObjectInputSt字节流的功能流 先序列化后反序列化 读入与写出的顺序保持一致 不是所有类型的对象都能实现序列化,要求实现一个java.io.Serializable 属性可以不被序列化保存 为属性设置transient 静态内容不能被序列化 当父类实现了序列化,子类没有属性,子类可以序列化所有内容 当子类实现了序列化,父类没有实现,子类只能序列化自己独有的内容ream 新增功能: Object readObject() 从ObjectInputStream中读取一个对象。
public class Class01_Object { public static void main(String[] args) throws IOException, ClassNotFoundException { //testOut("D://test.txt"); testInt("D://test.txt"); } //反序列化输入 //反序列化输入流 public static void testInt(String path) throws IOException, ClassNotFoundException { ObjectInputStream is = new ObjectInputStream(new FileInputStream(path)); //读入 int[] ints = (int[]) is.readObject(); Person person = (Person) is.readObject(); //处理数据 System.out.println(Arrays.toString(ints)); System.out.println(person); //关闭 is.close(); person.setName("丁天赐"); } //序列化输出 public static void testOut(String path) throws IOException { //序列化输出流 ObjectOutputStream in = new ObjectOutputStream(new FileOutputStream(path)); //准备数据 int[] arr = {1,2,3,4,5,6,7}; Person person = new Person("老范",22,12306); //写出 in.writeObject(arr); in.writeObject(person); //刷出 in.flush(); //关闭 in.close(); person.setAge(20); } } //定义一个Person类 class Person implements Serializable{ private static final long serialVersionUID = -414805216704048980L; private String name; private static int age; private int id; public Person() { } public Person(String name, int age, int id) { this.name = name; this.age = age; this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", id=" + id + '}'; } }
6.5.8序列号
1,序列号 : 实现了序列化的类中会默认生成序列号 可以通过序列号检测版本不一致问题,可以通过手动设置序列号解决版本问题 当类中的成员有变动,序列号会默认更新 2, 如何手动生成序列号: 1.实现序列化接口 2.setting设置->看截图 3.选中类名,alt+enter-->快捷键生成序列号
class Person implements Serializable { private static final long serialVersionUID = 2726196048396705113L; private transient int id; private String name; private static int age;
6.5.9 Commons-IO
1,CommonsIO是apache的一个开源的工具包,封装了IO操作的相关类
2,使用步骤: 1.去apache下下载CommonsIO的源码jar 2.项目下新建一个文件夹lib,jar包放入lib文件夹下 3.选中jar包右键add as lib... 4.项目下使用
public class Class01_CommonsIo { public static void main(String[] args) throws IOException { //IOUtils //IOUtils.copy(InputStream input, OutputStream output) // 此方法有多个重载方法,满足不同的输入输出流 IOUtils.copy(new FileInputStream("D://haha.txt"),new FileOutputStream("D://haha2.txt")); //FilenameUtils System.out.println(FilenameUtils.getName("D://AAA/haha.txt")); System.out.println(FilenameUtils.getBaseName("D://AAA/haha2.txt")); System.out.println(FilenameUtils.isExtension("D://AAA/test.txt","txt")); //FileUtils //FileUtils.copyFile(File srcFile, File destFile) // 复制文件 FileUtils.copyFile(new File("D://haha.txt"),FileUtils.getFile("D://haha1.txt")); //FileUtils.writeStringToFile(File file, String data, String encoding) // 字符串以指定的编码写到文件 FileUtils.writeStringToFile(new File("D://haha.txt"),"努力不一定会成功,但一定会有结果!!!","utf-8"); //FileUtils.copyDirectory(File srcDir, File destDir) // 复制文件夹(文件夹里面的文件内容也会复制) FileUtils.copyDirectory(new File("D://A"),new File("D://B")); //FileUtils.copyDirectory(File srcDir, File destDir, FileFilter filter) // 复制文件夹,带有文件过滤功能 FileUtils.copyDirectory(new File("D:A"),new File("D://B//A"),FileFilterUtils.directoryFileFilter()); } }