IO流:存储和读取数据的解决方案
import java.io.FileOutputStream;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {//1.创建对象//写出 输入流 OutputStream//本地文件fileFileOutputStream fos =new FileOutputStream("basic-code\\1.txt");//写出数据fos.write(97);//释放资源fos.close();}
}
import java.io.FileOutputStream;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {FileOutputStream fos = new FileOutputStream("basic-code\\1.txt");//fos.write(97);//fos.write(98);byte[] bytes={97,98,99,100,101};//fos.write(bytes);fos.write(bytes,1,3);fos.close();}
}
import java.io.FileOutputStream;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {FileOutputStream fos = new FileOutputStream("basic-code\\1.txt");String str ="yjybainchangsigema";byte[] bytes = str.getBytes();// System.out.println(Arrays.toString(bytes));fos.write(bytes);String str2 = "\r\n";byte[] bytes1 = str2.getBytes();fos.write(bytes1);String str3 = "come on";byte[] bytes2 = str3.getBytes();fos.write(bytes2);fos.close();}
}
import java.io.FileInputStream;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {FileInputStream fio =new FileInputStream("basic-code\\1.txt");int read = fio.read();System.out.println(read);fio.close();}
}
import java.io.FileInputStream;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {//字节输入流循环读取FileInputStream fis =new FileInputStream("baisc-code\\1.txt");int b;/** read:表示读取数据,而且是读取一个数据就移动一次指针* 如果没有变量b* 假设文件数据abcde* while((fis.read())!=-1){System.out.println(fis.read);* 相当于a!=-1 输出b* c!=-1 输出d* e!=-1 输出-1* 因此第三方变量必须写* */while((b=fis.read())!=-1){System.out.println((char)b);}fis.close();}
}
文件拷贝的基本代码(小文件)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {//文件拷贝//1.创建对象FileInputStream fis =new FileInputStream("D:\\iii\\movie.mp4");FileOutputStream fos = new FileOutputStream("basic-code\\copy.mp4");//2.拷贝//核心思想:边读边想int b;while((b =fis.read())!=-1){fos.write(b);}//3.释放资源//规则:先开的最后关闭fos.close();fis.close();}
}
import java.io.FileInputStream;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {//1.创建对象FileInputStream fis = new FileInputStream("basic-code\\1.txt");//2.读取数据byte[] bytes = new byte[2];//一次读取多个字节数据 具体多少 跟数组的长度有关//返回值 本子读取到多少个字节数据int len1 = fis.read(bytes);System.out.println(len1);//2String str1 = new String(bytes);System.out.println(str1);int len2 = fis.read(bytes);System.out.println(len2);//2String str2 = new String(bytes);System.out.println(str2);int len3 = fis.read(bytes);System.out.println(len3);//2String str3 = new String(bytes);System.out.println(str3);fis.close();}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {//文件拷贝改写long start = System.currentTimeMillis();FileInputStream fis = new FileInputStream("D:\\aaa");FileOutputStream fos = new FileOutputStream("baisc-code\\a");int len;byte[] bytes =new byte[1025*1024*5];while((len=fis.read())!=-1){fos.write(bytes,0,len);}fos.close();fis.close();long end = System.currentTimeMillis();System.out.println(end-start);}
}
字符集详解(ASCII, GBK)
import java.io.FileInputStream;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {//字节流读取中文会出现乱码FileInputStream fis = new FileInputStream("basic-code\\a");int b;while((b= fis.read())!=-1){System.out.println((char)b);}fis.close();}
}
数据流一个个拷贝 肯定就会出现乱码
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {//1.创建对象FileInputStream fis = new FileInputStream("basic-code\\a.txt");FileOutputStream fos =new FileOutputStream("basic-code\\1.txt");//2.拷贝int b;while((b=fis.read())!=-1){fos.write(b);}//3.释放资源fos.close();fis.close();}
}
记事本!数据不会丢失目的地和数据源保持一致
import java.io.IOException;
import java.util.Arrays;public class Test {public static void main(String[] args) throws IOException {//1.编码String str ="ai你哟";byte[] bytes1 = str.getBytes();System.out.println(Arrays.toString(bytes1));//???byte[] bytes2 = str.getBytes("GBK");System.out.println(Arrays.toString(bytes2));//???//2.解码String str2 = new String(bytes1);System.out.println(str2);String str3 = new String(bytes1,"GBK");System.out.println(str3);}
}
import java.io.FileReader;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {//1.创建对象并关联本地文件FileReader fr = new FileReader("basic-code\\1.txt");//2.读取数据 read()//字符流的底层也是字节流,默认也是一个字节一个字节的读取的.//如果遇到中文就会一次读取多个 GBK依次读两个字节,UTF-8一次读三个字节//在读取之后 方法的底层还会进行解码并转成十进制//最终把这个十进制作为返回值//这个十进制的数据也表示在字符集上的数字// 英文:文件里面二进制数据 0110 0001//read方法进行获取a,解码并转成十进制97//中文:文件里面的二进制数据 11100110 10110001 10001001//read方法进行读取,解码并转成十进制27721//我想看到中文汉字 就是把这些十进制数据 进行强转int ch;while((ch=fr.read())!=-1){System.out.print((char)ch);}//释放资源fr.close();}
}
import java.io.FileReader;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {//1.创建对象FileReader fr =new FileReader("basic-code\\1.txt");//2.读取数据char[] chars =new char[2];int len;//fr.read();//读取数据,解码,强转三步合并了,把强转之后的字符放到数组当中//空参的read()+强制类型转换while((len = fr.read(chars))!=-1){//把数组中的数据变成字符串再进行打印System.out.println(new String(chars,0,len));}//3.释放资源fr.close();}
}
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {//字节输出流:FileOutputStream fos = new FileOutputStream("basic-code\\1.txt");fos.write(97);//字节流 每次只能操作一个字节 255fos.close();//字符输出流:FileWriter fw =new FileWriter("basic-code\\a.txt");fw.write(7777);//根据字符集的编码方式进行编码 把编码之后的数据写到文件中去fw.close();FileWriter fw1= new FileWriter("basic-code\\1.txt",true);//续写开关//fw1.write("你好");char[] chars ={'a','b','c','我'};fw.write(chars);fw1.close();}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {//拷贝文件夹 考虑子文件//1.创建对象表示数据源File src =new File("D:\\aaa\\src");//2.创建对象表示目的地File dest = new File("D:\\aaa\\dest");//调用方法开始拷贝copydir(src,dest);}/** 作用:拷贝文件夹* 参数一:数据源* 参数二:目的地** */private static void copydir(File src, File dest) throws IOException {dest.mkdirs();递归//进入数据源File[] files = src.listFiles();//遍历数组for (File file : files) {if(file.isFile()){//判断文件 拷贝 FileInputStream fis =new FileInputStream(file);FileOutputStream fos =new FileOutputStream(new File(dest,file.getName()));byte[] bytes = new byte[1024];int len;while((len=fis.read(bytes))!=-1){fos.write(bytes,0,len);}fos.close();fis.close();}else{//判断文件夹 递归copydir(file,new File(dest,file.getName()));}}}
}
//文件加密//原始文件FileInputStream fis =new FileInputStream("C:\\girl.jpg");//加密处理FileOutputStream fos =new FileOutputStream("C:\\ency.jpg");//加密处理int b;while((b=fis.read())!=-1){fos.write(b ^2);//随便异或一个数字}fos.close();fis.close();//这个程序操作完成后就可以将源文件删除了 这样别人也不知道是什么东西等到你想看了//你再进行解密
//前置知识/** ^:异或* 两边相同:false* 两边不同:true* 0:false* 1:true** 100:110100* 10:1010** 1100100* ^0001010* ________________* 1101110 --->110* ^ 0001010* ________________* 1100100 --->100* System.out.println(100^10);//110* System.out.println(100^10^10);//100** *///文件加密//原始文件FileInputStream fis =new FileInputStream("C:\\ency.jpg");//加密处理FileOutputStream fos =new FileOutputStream("C:\\redu.jpg");//加密处理int b;while((b=fis.read())!=-1){fos.write(b ^2);//随便异或一个数字}fos.close();fis.close();//这个程序操作完成后就可以将源文件删除了 这样别人也不知道是什么东西等到你想看了//你再进行解密
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;public class Test {public static void main(String[] args) throws IOException {//修改文件中的数据//1.读取数据FileReader fr =new FileReader("basic-code\\a.txt");StringBuilder sb =new StringBuilder();int ch;while((ch = fr.read())!=-1){sb.append((char)ch);}fr.close();System.out.println(sb);//2.排序String str =sb.toString();String[] arrstr =str.split("-");ArrayList<Integer>list = new ArrayList<>();for (String s : arrstr) {int i = Integer.parseInt(s);list.add(i);}//System.out.println(list);Collections.sort(list);//3.写出FileWriter fw =new FileWriter("basic-code\\a.txt");for (int i = 0; i < list.size(); i++) {if(i==list.size()-1){fw.write(list.get(i)+"");//如果直接写数字 文件会变成ASCII码}else{fw.write(list.get(i)+"-");}}fw.close();}
}
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
//细节:文件中的数据不要换行 如果加了换行会有\r\n 读取也会读到
//bom头---占三个字节 编码改为ANSI
public class Test {public static void main(String[] args) throws IOException {//修改文件中的数据//1.读取数据FileReader fr =new FileReader("basic-code\\a.txt");StringBuilder sb =new StringBuilder();int ch;while((ch = fr.read())!=-1){sb.append((char)ch);}fr.close();System.out.println(sb);//2.排序Integer[] arr = Arrays.stream(sb.toString().split("-")).map(Integer::parseInt).sorted().toArray(Integer[]::new);//System.out.println(arr);//3.写出FileWriter fw = new FileWriter("C:\\yjy");String s = Arrays.toString(arr).replace(", ","-");String result = s.substring(1,s.length()-1);//System.out.println(result);fw.write(result);fw.close();}
}
public class Test {public static void main(String[] args) throws IOException {//1.创建缓冲流的对象BufferedInputStream bis = new BufferedInputStream(new FileInputStream("basic-code\\a.txt"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("basic-code\\1.txt"));//2.循环读取int b;while ((b = bis.read()) != -1) {bos.write(b);}//3.释放资源bos.close();bis.close();}
}
public class Test {public static void main(String[] args) throws IOException {//一次读写一个字节数组BufferedInputStream bis = new BufferedInputStream(new FileInputStream("basic-code\\a.txt"));BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream("basic-code\\b.txt"));byte[] bytes =new byte[1024];int len;while((len=bis.read(bytes))!=-1){bos.write(bytes,0,len);}bos.close();bis.close();}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;public class Test {public static void main(String[] args) throws IOException {//两个缓冲区之间这不也是一个字节一个字节的进行倒手的吗?? 速度为什么快了?//因为这一段是在内存中运行的速度非常的快/*字符缓冲输入流:* 构造方法:public BufferedReader(Reader r)* 特有方法:public String readLine() 读一整行*///readLine 方法在读取的时候 一次读一整行 遇到空格换行结束//但是他不会把回车读到内存当中 //1.创建字符缓冲输入流的对象BufferedReader br =new BufferedReader(new FileReader("basic-code\\1.txt"));//2.读取数据
// String line = br.readLine();
// System.out.println(line);String line;while((line=br.readLine())!=null){System.out.println(line);}//释放资源br.close();}
}
public class Test {public static void main(String[] args) throws IOException {/*字符缓冲输出流* 构造方法:public BufferedWriter(writer r)* 特有方法:public void newLine() 跨平台换行*///1.创建字符缓冲输出流的对象BufferedWriter bw =new BufferedWriter(new FileWriter("b.txt"));//2.写出数据bw.write("你嘴角上扬的样子,百度搜索不到");bw.newLine();bw.write("以后我结婚你一定要来 ,没有新娘我会很尴尬");//3.释放资源bw.close();}
}
import java.io.*;public class Test {public static void main(String[] args) throws IOException {//拷贝文件 //四种方法拷贝文件,并统计各自用时//字节流的基本流:依次读写一个字节//字节流的基本流:一次读写一个字节数组//字节缓冲流:一次读写一个字节//字节缓冲流:一次读写一个字节数组long start = System.currentTimeMillis();method1();method2();//16method3();//95method4();//17long end=System.currentTimeMillis();System.out.println((end-start)/1000.0+"秒");}private static void method3() throws IOException {//字节缓冲流BufferedInputStream bis =new BufferedInputStream(new FileInputStream("1.txt"));BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream("2.txt"));int b;while((b=bis.read())!=-1){bos.write(b);}bos.close();bis.close();}private static void method4() throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream("1.txt"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("2.txt"));byte[] bytes = new byte[8192];int len;while ((len = bis.read(bytes)) != -1) {bos.write(bytes,0,len);}bos.close();bis.close();}private static void method2() throws IOException {FileInputStream fis = new FileInputStream("C:\\1.txt");FileOutputStream fos=new FileOutputStream("1.txt");byte[] bytes= new byte[1024];int len;while((len=fis.read(bytes))!=-1){fos.write(bytes,0,len);}fos.close();fis.close();}private static void method1() throws IOException {FileInputStream fis = new FileInputStream("C:\\1.txt");FileOutputStream fos = new FileOutputStream("1.txt");int b;while ((b = fis.read()) != -1) {fos.write(b);}fos.close();fis.close();}}
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;public class Test {public static void main(String[] args) throws IOException {//需求:把<出师表>的文章顺序进行恢复到一个新文件中//1.读取数据BufferedReader br =new BufferedReader(new FileReader("basic-code"));String line;ArrayList<String>list=new ArrayList<>();while((line=br.readLine())!=null){// System.out.println(line);list.add(line);}br.close();//2.排序//排序规则:按照每一行前面的序号进行排序Collections.sort(list, new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {//获取o1,o2的序号int i = Integer.parseInt(o1.split("\\.")[0]);int i1 = Integer.parseInt(o2.split("\\.")[0]);return i-i1;}});//写出BufferedWriter bw =new BufferedWriter(new FileWriter("1.txt"));for (String s : list) {bw.write(s);bw.newLine();}bw.close();}
}
import java.io.*;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;public class Test {public static void main(String[] args) throws IOException {//需求:把<出师表>的文章顺序进行恢复到一个新文件中BufferedReader br =new BufferedReader(new FileReader("1.txt"));String line;TreeMap<Integer,String> tm = new TreeMap<>();while((line=br.readLine())!=null){String[] arr =line.split("\\.");//0:序号 1:内容tm.put(Integer.parseInt(arr[0]),line);}br.close();//System.out.println(tm);//2.写出数据BufferedWriter bw =new BufferedWriter(new FileWriter("basic-code"));Set<Map.Entry<Integer,String>>entries = tm.entrySet();for(Map.Entry<Integer,String>entry:entries){String value = entry.getValue();bw.write(value);bw.newLine();}bw.close();}
}
import java.io.*;public class Test {public static void main(String[] args) throws IOException {//能用计数器的知识来做吗?不能 因为变量创建在内存中 程序重启 又能够再来三次//为了永久化存储所以要保存在文件当中//1.把文件中的数字读取到内存中//一次读一行 而不是读一个BufferedReader br =new BufferedReader(new FileReader("basic-code"));String line =br.readLine();int count = Integer.parseInt(line);//表示当前软件又运行了一次count++;//2.判断if(count<=3){System.out.println("欢迎使用本软件,第"+count+"次免费使用");}else{System.out.println("本软件只能使用三次,请注册会员继续使用");}//<=3:正常运行//>3:不能运行//3.把自增之后的count写出到文件当中BufferedWriter bw = new BufferedWriter(new FileWriter("basic-code"));//缓冲区输出流在关联文件的时候,文件存在就会清空//原则:IO:随用随创建//什么时候不用就关掉bw.write(count+"");//97 abw.close();}
}
import java.io.*;
import java.nio.charset.Charset;public class Test {public static void main(String[] args) throws IOException {//利用转换流按照指定字符编码读取(了解)/** 因为JDK11:这种方法被淘汰了 --->替代方案(掌握)*/// //1.创建对象并指定字符编码(了解即可)
// InputStreamReader isr =new InputStreamReader(new FileInputStream("basic-code"),"GBK");
// //2.读取数据
// int ch;
// while((ch=isr.read())!=-1){
// System.out.println((char)ch);
// }
// //3.释放资源
// isr.close();//掌握FileReader fr = new FileReader("basic-code", Charset.forName("GBK"));// 2.读取数据int ch;while((ch=fr.read())!=-1){System.out.println((char)ch);}//3.释放资源fr.close();}
}
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;public class Test {public static void main(String[] args) throws IOException {//利用转换流按照指定字符编码写出(了解)// //1.创建转换流对象
// OutputStreamWriter osw =new OutputStreamWriter(new FileOutputStream("basic-code"),"GBK");
// //2.写出数据
// osw.write("你好你好");
// //3.释放资源
// osw.close();//替代方案FileWriter fw = new FileWriter("basic-code", Charset.forName("GBK"));fw.write("你好你好");fw.close();}
}
import java.io.*;
import java.nio.charset.Charset;public class Test {public static void main(String[] args) throws IOException {//将本地文件中GBK文件 转成UTF-8//1.JDK以前的方案InputStreamReader isr = new InputStreamReader(new FileInputStream("basic-code"),"GBK");OutputStreamWriter osw =new OutputStreamWriter(new FileOutputStream("basic-code1"),"UTF-8");int b;while ((b= isr.read())!=-1){osw.write(b);}osw.close();isr.close();//替代方案FileReader fr =new FileReader("basic-code", Charset.forName("GBK"));FileWriter fw =new FileWriter("basic-code1",Charset.forName("UTF-8"));int b;while((b=fr.read())!=-1){fw.write(b);}fw.close();fr.close();}
}
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;public class Test {public static void main(String[] args) throws IOException {//利用字节流读取文件中的数据 每次读一整行,而且不能出现乱码//1.字节流在读取中文的时候,是会出现乱码的,但是字符流可以搞定//2.字节流里面没有读一整行的方法的 只有字符缓冲流可以搞定// FileInputStream fis= new FileInputStream("basic-code");
// InputStreamReader isr =new InputStreamReader(fis);
//
// BufferedReader br =new BufferedReader(isr);
// String s = br.readLine();
// br.close();BufferedReader br =new BufferedReader(new InputStreamReader(new FileInputStream("yjy.txt")));String line;while((line=br.readLine())!=null){System.out.println(line);}br.close();}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;public class Test {public static void main(String[] args) throws IOException {/** serializable接口里面没有抽象方法,标记型接口* 一旦实现了这个接口 那么就表示当前的student类可以被序列化* 理解:* 一个物品的合格证* *///序列化流Student stu =new Student("zhangsan",23);ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("basic-code\\a.txt"));oos.writeObject(stu);oos.close();}
}
import java.io.Serializable;public class Student implements Serializable {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}/*** 获取* @return name*/public String getName() {return name;}/*** 设置* @param name*/public void setName(String name) {this.name = name;}/*** 获取* @return age*/public int getAge() {return age;}/*** 设置* @param age*/public void setAge(int age) {this.age = age;}public String toString() {return "Student{name = " + name + ", age = " + age + "}";}
}
自己设置一个提示-版本号
在搜索框搜索Serializable
public class Student implements Serializable {@Serialprivate static final long serialVersionUID = 5498121245996411513L;// private static final long serialVersionUID = 1L;private String name;private int age;private String address;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}/*** 获取* @return name*/public String getName() {return name;}/*** 设置* @param name*/public void setName(String name) {this.name = name;}/*** 获取* @return age*/public int getAge() {return age;}/*** 设置* @param age*/public void setAge(int age) {this.age = age;}public String toString() {return "Student{name = " + name + ", age = " + age + "}";}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;public class Test {public static void main(String[] args) throws IOException, ClassNotFoundException {///用对象流读写多个对象//需求://将多个自定义对象序列化到文件中,但是由于对象的个数不确定,反序列化流该如何读取呢?Student s1 =new Student("zhangsan",23,"南京");Student s2 =new Student("lisi",24,"重庆");Student s3 =new Student("wangwu",25,"北京");//序列化多个对象ArrayList<Student>list =new ArrayList<>();list.add(s1);list.add(s2);list.add(s3);ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("basic-code"));oos.writeObject(list);oos.close();}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;public class Demo {public static void main(String[] args) throws IOException, ClassNotFoundException {//反序列化流的对象ObjectInputStream ois = new ObjectInputStream(new FileInputStream("basic-code"));
//
// Student s1 = (Student) ois.readObject();
// Student s2 = (Student) ois.readObject();
// Student s3 = (Student) ois.readObject();ArrayList<Student>list = (ArrayList<Student>) ois.readObject();for (Student student : list) {System.out.println(student);}ois.close();}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;public class Test {public static void main(String[] args) throws IOException, ClassNotFoundException {//打印流//1.创建字节打印流的对象PrintStream ps =new PrintStream(new FileOutputStream("myio\\1.txt"),true, Charset.forName("UTF-8"));//2.写出数据ps.println(97);//写出+自动刷新+自动换行ps.print(true);ps.println();ps.printf("%s 爱上了 %s","阿珍","阿强");//3.释放资源ps.close();}
}
public class Test {public static void main(String[] args) throws IOException, ClassNotFoundException {//打印流//1.创建字符打印流PrintWriter pw =new PrintWriter(new FileWriter("basic-code"),true);pw.println("dhuaidhwahdo");pw.print("你好你好");pw.close();}
}
解压缩流/压缩流
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;public class Test {public static void main(String[] args) throws IOException, ClassNotFoundException {//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对象// for (int i = 0; i < 100; i++) {
// ZipEntry entry = zip.getNextEntry();
// System.out.println(entry);//找不到就是返回null
// }//表示当前在压缩包中获取到的文件或者文件夹ZipEntry entry;while ((entry = zip.getNextEntry())!=null){System.out.println(entry);//文件夹:需要在目的地dest处创建一个同样的文件夹//文件:需要读取到压缩包中的文件,并把他存放到目的地dest文件夹中(按照层级目录进行存放)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();}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class Test {public static void main(String[] args) throws IOException, ClassNotFoundException {//压缩单个文件 ->压缩包//1.创建File对象表示要压缩的文件File src = new File("D:\\yjy.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,"yjy.zip")));//2.创建ZipEntry对象,表示压缩包里面的每一个文件和文件夹ZipEntry entry = new ZipEntry("yjy.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();}
}
import java.io.File;
import java.io.IOException;public class DEMO {public static void main(String[] args) throws IOException {
// File src =new File("basic-code\\1.txt");
// File dest =new File("basic-code\\copy.txt");
//
// FileUtils.copyFile(src,dest);//用这个包直接拷贝了// File src =new File("D:\\aaa");
// File dest =new File("D:\\bbb");
// FileUtils.copyDirectory(src,dest);// File src =new File("D:\\aaa");
// File dest =new File("D:\\bbb");
// FileUtils.copyDirectoryToDirectory(src,dest);//作用:bbb:里面有aaa然后有aaa里面的东西File src =new File("D:\\aaa");FileUtils.deleteDirectory(src);File dest =new File("D:\\bbb");FileUtils.cleanDirectory(dest);}
}
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class DEMO {public static void main(String[] args) throws IOException {//Hutool//因为里面的方法都是静态的所以直接用类名就可以调用了File file = FileUtil.file("D:\\aaa", "bbb", "a.txt");System.out.println(file);//D:\aaa\bbb\a.txtFile f =new File("a.txt");f.createNewFile();//如果父级路径不存在那么会报错//Hutool 里面的touch方法如果找不到这个父级路径会帮我们创建File touch = FileUtil.touch(file);System.out.println(touch);ArrayList<String>list =new ArrayList<>();list.add("aaa");list.add("aaa");list.add("aaa");File file1 = FileUtil.writeLines(list, "D:\\a.txt", "UTF-8", false);System.out.println(file1);File file2 = FileUtil.appendLines(list, "D:\\a.txt", "UTF-8");System.out.println(file2);List<String> strings = FileUtil.readLines("D:\\a.txt", "UTF-8");System.out.println(list);}
}
官网:
https://hutool.cn/
API文档:
https://apidoc.gitee.com/dromara/hutool/
中文使用文档:
https://hutool.cn/docs/#/