4月18号

输入输出流一值:

FileReader fr = new FileReader(fileName:"myiolIb.txt", Charset.forName("GBK"));
FileWriter fw = new FileWriter(fileName:"myiolle.txt",Charset.forName("UTF-8"));
int b;
while ((b = fr.read()) != -1){
    fw.write(b);
}
fw.close();
fr.close();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(name: "myiolIa.txt")));
String line;
while ((line = br.readLine()) != null){
    System.out.println(line);
}
br.close();

//1.创建对象
Student stu = new Student(name: "zhangsan", age: 23);

//2.创建序列化流的对象/对象操作输出流
ObjectoutputStream oos = new ObjectOutputStream(new FileOutputStream(name:"myiolla.txt");

//3.写出数据
oos.writeObject(stu);
//4.释放资源
oos.close();

//1.创建反序列化流的对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(name:"myiolla.txt"));

//2.读取数据
Object o = ois.readobject();

//3.打印对象
System.out.println(o);

//4.释放资源
ois.close();

解压:

//定义一个方法用来解压

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();
         }
    }
}

压缩:

public static void toZip(File src,File dest) throws IOException {
//1.创建压缩流关联压缩包
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest,child:"a.zip")));
    //2.创建ZipEntry对象,表示压缩包里面的每一个文件和文件夹
    ZipEntry entry = new ZipEntry( name: "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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值