输入输出流一值:
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();
}