JAVA—IO高级流

JAVA—IO高级流 缓冲流字节缓冲流底层自带长度为8192的缓冲区提高性能字节缓冲流包括BufferedInputStream字节缓冲输入流、BufferedInputStream字节缓冲输出流构造方法public BufferedInputStream(InputStream in)把基本流包装成高级流提高读取数据性能public BufferedOutputStream(OutputStream out) 把基本流包装成高级流提高写出数据性能public class demo { public static void main(String[] args) throws IOException { //拷贝文件 //1.创建缓冲流对象 BufferedInputStream bisnew BufferedInputStream(new FileInputStream(a.txt)); BufferedOutputStream bosnew BufferedOutputStream(new FileOutputStream(copy.txt)); //2.循环读取写到目的地 int b; while((bbis.read())!-1){ bos.write(b); } //3.关闭流 bis.close(); bos.close(); //一次读多个字节 byte[] bytesnew byte[1024]; int len; while((lenbis.read(bytes))!-1){ bos.write(bytes,0,len); } } }字符缓冲流特有方法BufferedReaderpublic String readLine(): 读一行数据如果没有数据可读返回nullreadline方法读取时一次读一整行遇到回车换行结束但不会把回车换行读取到内存BufferedWriterpublic void newLine():跨平台换行//输入流 public class demo { public static void main(String[] args) throws IOException { BufferedReader br new BufferedReader(new FileReader(a.txt)); //读取数据 //readline方法读取时一次读一整行遇到回车换行结束但不会把回车换行读取到内存 String linebr.readLine(); System.out.println(line); //循环读取 while((linebr.readLine())!null){ System.out.println(line); } br.close(); } }//输出流 public class demo { public static void main(String[] args) throws IOException { //true:续写 BufferedWriter bwnew BufferedWriter(new FileWriter(b.txt,true)); bw.write(123456789); bw.newLine();//换行 bw.write(1011121314); bw.newLine(); bw.close(); } }转换流转换流是字符流和字节流之间的桥梁//指定编码读取 public class ReaderDemo2 { public static void main(String[] args) throws IOException { // 定义文件路径,文件为gbk编码 String FileName E:\\file_gbk.txt; // 创建流对象,默认UTF8编码 InputStreamReader isr new InputStreamReader(new FileInputStream(FileName)); // 创建流对象,指定GBK编码 InputStreamReader isr2 new InputStreamReader(new FileInputStream(FileName) , GBK); // 定义变量,保存字符 int read; // 使用默认编码字符流读取,乱码 while ((read isr.read()) ! -1) { System.out.print((char)read); // Һ } isr.close(); // 使用指定编码字符流读取,正常解析 while ((read isr2.read()) ! -1) { System.out.print((char)read);// 大家好 } isr2.close(); } } //指定编码写出 public class OutputDemo { public static void main(String[] args) throws IOException { // 定义文件路径 String FileName E:\\out.txt; // 创建流对象,默认UTF8编码 OutputStreamWriter osw new OutputStreamWriter(new FileOutputStream(FileName)); // 写出数据 osw.write(你好); // 保存为6个字节 osw.close(); // 定义文件路径 String FileName2 E:\\out2.txt; // 创建流对象,指定GBK编码 OutputStreamWriter osw2 new OutputStreamWriter(new FileOutputStream(FileName2),GBK); // 写出数据 osw2.write(你好);// 保存为4个字节 osw2.close(); } } //将GBK编码的文本文件转换为UTF-8编码的文本文件 public class TransDemo { public static void main(String[] args) { // 1.定义文件路径 String srcFile file_gbk.txt; String destFile file_utf8.txt; // 2.创建流对象 // 2.1 转换输入流,指定GBK编码 InputStreamReader isr new InputStreamReader(new FileInputStream(srcFile) , GBK); // 2.2 转换输出流,默认utf8编码 OutputStreamWriter osw new OutputStreamWriter(new FileOutputStream(destFile)); // 3.读写数据 // 3.1 定义数组 char[] cbuf new char[1024]; // 3.2 定义长度 int len; // 3.3 循环读取 while ((len isr.read(cbuf))!-1) { // 循环写出 osw.write(cbuf,0,len); } // 4.释放资源 osw.close(); isr.close(); } }序列化流ObjectOutputStream可以将java中对象写到本地文件中public ObjectOutputStream(OutputStream out) 创建一个指定OutputStream的ObjectOutputStreampublic final void writeObject (Object obj): 将指定的对象写出一个对象要想序列化必须满足两个条件:该类必须实现java.io.Serializable接口Serializable是一个标记接口没有抽象方法不实现此接口的类将不会使任何状态序列化或反序列化会抛出NotSerializableException。该类的所有属性必须是可序列化的。如果有一个属性不需要可序列化的则该属性必须注明是瞬态的使用transient关键字修饰。public class Employee implements java.io.Serializable { // 固定序列版本号方便后续修改代码 private static final long serialVersionUID 1L; public String name; public String address; public transient int age; // transient瞬态修饰成员,不会被序列化 public void addressCheck() { System.out.println(Address check : name -- address); } } public class SerializeDemo{ public static void main(String [] args) { Employee e new Employee(); e.name zhangsan; e.address beiqinglu; e.age 20; try { // 创建序列化流对象 ObjectOutputStream out new ObjectOutputStream(new FileOutputStream(employee.txt)); // 写出对象 out.writeObject(e); // 释放资源 out.close(); fileOut.close(); System.out.println(Serialized data is saved); // 姓名地址被序列化年龄没有被序列化。 } catch(IOException i) { i.printStackTrace(); } } } 输出结果 Serialized data is savedObjectInputStream反序列化流将之前使用ObjectOutputStream序列化的原始数据恢复为对象public ObjectInputStream(InputStream in) 创建一个指定InputStream的ObjectInputStreampublic final Object readObject (): 读取一个对象public class DeserializeDemo { public static void main(String [] args) { Employee e null; try { // 创建反序列化流 FileInputStream fileIn new FileInputStream(employee.txt); ObjectInputStream in new ObjectInputStream(fileIn); // 读取一个对象 e (Employee) in.readObject(); // 释放资源 in.close(); fileIn.close(); }catch(IOException i) { // 捕获其他异常 i.printStackTrace(); return; }catch(ClassNotFoundException c) { // 捕获类找不到异常 System.out.println(Employee class not found); c.printStackTrace(); return; } // 无异常,直接打印输出 System.out.println(Name: e.name); // zhangsan System.out.println(Address: e.address); // beiqinglu System.out.println(age: e.age); // 0 } }打印流public PrintStream(String fileName) 使用指定的文件名创建一个新的打印流public class PrintDemo { public static void main(String[] args) throws IOException { // 调用系统的打印流,控制台直接输出97 System.out.println(97); // 创建打印流,指定文件的名称 PrintStream ps new PrintStream(ps.txt); // 设置系统的打印流流向,输出到ps.txt System.setOut(ps); // 调用系统的打印流,ps.txt中输出97 System.out.println(97); } }压缩流解压流压缩流负责压缩文件或者文件夹解压缩流负责把压缩包中的文件和文件夹解压出来/* * 解压缩流 * * */ public class ZipStreamDemo1 { public static void main(String[] args) throws IOException { //1.创建一个File表示要解压的压缩包 File src new File(D:\\aaa.zip); //2.创建一个File表示解压的目的地 File dest new File(D:\\); //调用方法 unzip(src,dest); } //定义一个方法用来解压 public static void unzip(File src,File dest) throws IOException { //解压的本质把压缩包里面的每一个文件或者文件夹读取出来按照层级拷贝到目的地当中 //创建一个解压缩流用来读取压缩包中的数据 ZipInputStream zip new ZipInputStream(new FileInputStream(src)); //要先获取到压缩包里面的每一个zipentry对象 //表示当前在压缩包中获取到的文件或者文件夹 ZipEntry entry; while((entry zip.getNextEntry()) ! null){ System.out.println(entry); if(entry.isDirectory()){ //文件夹需要在目的地dest处创建一个同样的文件夹 File file new File(dest,entry.toString()); file.mkdirs(); }else{ //文件需要读取到压缩包中的文件并把他存放到目的地dest文件夹中按照层级目录进行存放 FileOutputStream fos new FileOutputStream(new File(dest,entry.toString())); int b; while((b zip.read()) ! -1){ //写到目的地 fos.write(b); } fos.close(); //表示在压缩包中的一个文件处理完毕了。 zip.closeEntry(); } } zip.close(); } }public class ZipStreamDemo2 { public static void main(String[] args) throws IOException { /* * 压缩流 * 需求 * 把D:\\a.txt打包成一个压缩包 * */ //1.创建File对象表示要压缩的文件 File src new File(D:\\a.txt); //2.创建File对象表示压缩包的位置 File dest new File(D:\\); //3.调用方法用来压缩 toZip(src,dest); } /* * 作用压缩 * 参数一表示要压缩的文件 * 参数二表示压缩包的位置 * */ public static void toZip(File src,File dest) throws IOException { //1.创建压缩流关联压缩包 ZipOutputStream zos new ZipOutputStream(new FileOutputStream(new File(dest,a.zip))); //2.创建ZipEntry对象表示压缩包里面的每一个文件和文件夹 //参数压缩包里的路径 ZipEntry entry new ZipEntry(a.txt); //3.把ZipEntry对象放到压缩包当中 zos.putNextEntry(entry); //4.把src文件中的数据写到压缩包当中 FileInputStream fis new FileInputStream(src); int b; while((b fis.read()) ! -1){ zos.write(b); } zos.closeEntry(); zos.close(); } }public class ZipStreamDemo3 { public static void main(String[] args) throws IOException { /* * 压缩流 * 需求 * 把D:\\aaa文件夹压缩成一个压缩包 * */ //1.创建File对象表示要压缩的文件夹 File src new File(D:\\aaa); //2.创建File对象表示压缩包放在哪里压缩包的父级路径 File destParent src.getParentFile();//D:\\ //3.创建File对象表示压缩包的路径 File dest new File(destParent,src.getName() .zip); //4.创建压缩流关联压缩包 ZipOutputStream zos new ZipOutputStream(new FileOutputStream(dest)); //5.获取src里面的每一个文件变成ZipEntry对象放入到压缩包当中 toZip(src,zos,src.getName());//aaa //6.释放资源 zos.close(); } /* * 作用获取src里面的每一个文件变成ZipEntry对象放入到压缩包当中 * 参数一数据源 * 参数二压缩流 * 参数三压缩包内部的路径 * */ public static void toZip(File src,ZipOutputStream zos,String name) throws IOException { //1.进入src文件夹 File[] files src.listFiles(); //2.遍历数组 for (File file : files) { if(file.isFile()){ //3.判断-文件变成ZipEntry对象放入到压缩包当中 ZipEntry entry new ZipEntry(name \\ file.getName());//aaa\\no1\\a.txt zos.putNextEntry(entry); //读取文件中的数据写到压缩包 FileInputStream fis new FileInputStream(file); int b; while((b fis.read()) ! -1){ zos.write(b); } fis.close(); zos.closeEntry(); }else{ //4.判断-文件夹递归 toZip(file,zos,name \\ file.getName()); // no1 aaa \\ no1 } } } }工具包Commons-io使用方式1新建lib文件夹2把第三方jar包粘贴到文件夹中3右键点击jar包add as a librarypublic class CommonsIODemo1 { public static void main(String[] args) throws IOException { /* FileUtils类 static void copyFile(File srcFile, File destFile) 复制文件 static void copyDirectory(File srcDir, File destDir) 复制文件夹 static void copyDirectoryToDirectory(File srcDir, File destDir) 复制文件夹 static void deleteDirectory(File directory) 删除文件夹 static void cleanDirectory(File directory) 清空文件夹 static String readFileToString(File file, Charset encoding) 读取文件中的数据变成成字符串 static void write(File file, CharSequence data, String encoding) 写出数据 IOUtils类 public static int copy(InputStream input, OutputStream output) 复制文件 public static int copyLarge(Reader input, Writer output) 复制大文件 public static String readLines(Reader input) 读取数据 public static void write(String data, OutputStream output) 写出数据 */ File src new File(myio\\a.txt); File dest new File(myio\\copy.txt); FileUtils.copyFile(src,dest); File src new File(D:\\aaa); File dest new File(D:\\bbb); FileUtils.copyDirectoryToDirectory(src,dest); File src new File(D:\\bbb); FileUtils.cleanDirectory(src); } }hutoolpublic class Test1 { public static void main(String[] args) { /* FileUtil类: file根据参数创建一个file对象 touch根据参数创建文件 writeLines把集合中的数据写出到文件中覆盖模式。 appendLines把集合中的数据写出到文件中续写模式。 readLines指定字符编码把文件中的数据读到集合中。 readUtf8Lines按照UTF-8的形式把文件中的数据读到集合中 copy拷贝文件或者文件夹 */ File file1 FileUtil.file(D:\\, aaa, bbb, a.txt); System.out.println(file1);//D:\aaa\bbb\a.txt File touch FileUtil.touch(file1); System.out.println(touch); ArrayListString list new ArrayList(); list.add(aaa); list.add(aaa); list.add(aaa); File file2 FileUtil.writeLines(list, D:\\a.txt, UTF-8); System.out.println(file2); ArrayListString list new ArrayList(); list.add(aaa); list.add(aaa); list.add(aaa); File file3 FileUtil.appendLines(list, D:\\a.txt, UTF-8); System.out.println(file3); ListString list FileUtil.readLines(D:\\a.txt, UTF-8); System.out.println(list); } }