Java---高级流

目录

一、转换流

(1)指定的字符集读写数据

二、序列化流和反序列化流

三、解压缩流和压缩流

(1)解压缩流

 (2)压缩流

一:压缩文件

二:压缩文件夹


注:本文并未介绍Java中的所有高级流,只介绍了三种!

一、转换流

转换流就是转换,它可以使字节流转换成字符流,也可以将字符流转换成字节流。
在读数据时InputStreamReader,就是将字节流转换成字符流
写数据时OutputStreamWriter,就是将字符流转换成字节流
他们的作用可以指定字符集读写数据,字节流可以使用字符流中的方法

(1)指定的字符集读写数据

public class exe_one {
    public static void main(String[] args) throws IOException {
      File src=new File("D:\\java\\8.txt");
      duqu(src,"GBK");
      xie(src,"GBK");
   }
   public static void duqu(File src,String charset) throws IOException {
        //读取src路径的文件,charset是指定的字符集

       //转换流
       InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream(src),charset);
       int b;
       while((b=inputStreamReader.read())!=-1){
           System.out.print((char)b);
       }
       inputStreamReader.close();
   }
   public static void xie(File src,String charset) throws IOException {
        //写到src文件,charset是指定的字符集
        OutputStreamWriter outputStreamWriter=new OutputStreamWriter(new FileOutputStream(src),charset);
        outputStreamWriter.write("一加一等于100");
        outputStreamWriter.close();
   }
}

二、序列化流和反序列化流

序列化流:ObjectOutputStream,它可以把Java中的对象
写到本地文件中,比如具体实例中游戏的存档,如果单纯将数据写进去,
那么用户就可以看懂并进行修改,序列化的作用就是将对象写进本地文件中,
并且是一串奇怪的字符,别人看不懂。
同时要注意一个小细节,使用序列化流保存文件一定要记得将javabean类实现Serializable接口

反序列化流:ObjectInputStream他的作用就是将序列化到本地文件中的对象读取到程序中

public class exe_one {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        File src=new File("D:\\java\\7.txt");
      //xulie(src);
      fanxulie(src);
   }
   public static void xulie(File src) throws IOException {
        //序列化流将对象保存到本地文件中
       Student s=new Student("张三",19);
       ObjectOutputStream outputStream=new ObjectOutputStream(new FileOutputStream(src));
       outputStream.writeObject(s);
       outputStream.close();
   }
   public static void fanxulie(File src) throws IOException, ClassNotFoundException {
        //反序列化
       ObjectInputStream o=new ObjectInputStream(new FileInputStream(src));
       Student s= (Student) o.readObject();
       System.out.println(s.name);
       o.close();
   }
}

 

三、解压缩流和压缩流

对于解压缩流和压缩流的定义其实很简单,就是解压和压缩

(1)解压缩流

解压本质:就是吧每一个ZipEntry按照层级拷贝到本地另外一个文件夹中
要注意这里的解压缩的格式后缀名一定要是zip,其他Java无法识别。
 

解压缩流基本代码:

//将一个a压缩包解压

public class exe_one {
    public static void main(String[] args) throws IOException {
       File src=new File("D:\\java\\a.zip");
       File dest=new File("D:\\java");
       jieyasuo(src,dest);
    }
    public static void jieyasuo(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()){
                //是文件夹
                    File fiel=new File(dest,entry.toString());
                    fiel.mkdirs();
            }
            else{
                //是文件
                //开始将压缩包中的数据写到指定的位置上去
                FileOutputStream outputStream=new FileOutputStream(new File(dest,entry.toString()));
                int b;
                while((b=zip.read())!=-1){
                    outputStream.write(b);
                }
                outputStream.close();
                zip.closeEntry();
            }
        }
        zip.close();
    }

}

 (2)压缩流

一:压缩文件

public class exe_one {
    public static void main(String[] args) throws IOException {
       File src=new File("D:\\C\\ssss.txt");
       File dest=new File("D:\\C");
       jieyasuo(src,dest);
    }
    public static void jieyasuo(File src,File dest) throws IOException {
        //将ssss.txt压缩成zip格式
        //创建压缩关流
        ZipOutputStream zipOutputStream=new ZipOutputStream(new FileOutputStream(new File(dest,"ssss.txt")));
        //创建zipEntry对象,表示压缩包中的每一个文件
        ZipEntry entry=new ZipEntry("ssss.txt");
        //将ZipEntry对象放到压缩包中
        zipOutputStream.putNextEntry(entry);
        //将src文件中的数据写到压缩包中
        FileInputStream fileInputStream=new FileInputStream(src);
        int b;
        while((b=fileInputStream.read())!=-1){
            zipOutputStream.write(b);
        }
        zipOutputStream.closeEntry();
        zipOutputStream.close();

    }

}

二:压缩文件夹

具体代码如下:

public class exe_one {
    public static void main(String[] args) throws IOException {
        File src=new File("D:\\java\\a");
        File destparent=src.getParentFile();//得到压缩文件的父级路径
        File dest=new File(destparent,src.getName()+".zip");

        //创建压缩关联包
        ZipOutputStream zipOutputStream=new ZipOutputStream(new FileOutputStream(dest));
        //获取src中的每个文件以及文件夹变成ZipEntry对象
        yasuo(src,zipOutputStream,src.getName());
        zipOutputStream.close();
    }
   public static void yasuo(File src,ZipOutputStream zipOutputStream,String name) throws IOException {
       File[] files=src.listFiles();
       for(File file:files){
           if(file.isFile()){
               //是文件
               //变成ZipEntrry对象,将数据压缩
               ZipEntry entry=new ZipEntry(name+"\\"+file.getName());//压缩到的路径
               zipOutputStream.putNextEntry(entry);
               //读取文件中的数据,写到压缩包中
               FileInputStream inputStream=new FileInputStream(file);
               int b;
               while((b=inputStream.read())!=-1){
                   zipOutputStream.write(b);
               }
               inputStream.close();
               zipOutputStream.closeEntry();
           }
           else{
               //文件夹压缩
               //递归压缩
               yasuo(file,zipOutputStream,name+"\\"+file.getName());
           }
       }
   }
}

猜你喜欢

转载自blog.csdn.net/gaoqiandr/article/details/129124666