• IO流(3)


    打印流

    字节打印流

    特有方法实现:数据原样写出。

    1. public class test {
    2. public static void main(String [] args) throws IOException, ClassNotFoundException {
    3. //打印流
    4. //创建字节打印流对象
    5. PrintStream ps=new PrintStream(new FileOutputStream("c.txt"), true, "UTF-8");
    6. ps.println(97);
    7. ps.print(true);
    8. ps.println();
    9. ps.printf("%s喜欢%s","z","w");
    10. ps.close();
    11. }
    12. }

    字符打印流

    1. public class test {
    2. public static void main(String [] args) throws IOException, ClassNotFoundException {
    3. //打印流
    4. //创建字符打印流
    5. PrintWriter pw=new PrintWriter(new FileWriter("c.txt"), true);
    6. pw.println("你好,java");
    7. pw.close();
    8. }
    9. }

    压缩流与解压缩流

    解压缩流

    解压本质:把每一个zipEntry按层级拷贝到本地另一个文件夹中。

    小练习:解压文件夹

    将“D:\Date\aaa.zip”解压到“D:\Date”

    1. public class test {
    2. public static void main(String [] args) throws IOException {
    3. //解压文件夹
    4. File src=new File("D:\\Date\\aaa.zip");//原压缩文件夹地址
    5. File dest=new File("D:\\Date");//目的文件夹地址
    6. unzip(src, dest);
    7. }
    8. //创建一个解压方法对文件夹进行解压
    9. public static void unzip(File src,File dest) throws IOException {
    10. //创建解压流对象:(读压缩流)
    11. ZipInputStream zip=new ZipInputStream(new FileInputStream(src));
    12. //利用while循环将文件夹中的每个对象解压出来,
    13. ZipEntry entry;
    14. while((entry=zip.getNextEntry())!=null) {
    15. //有文件和文件夹
    16. //对于文件和文件夹的处理方法不同,需要将二者分开处理
    17. if(entry.isDirectory()) {
    18. //文件夹
    19. //需要将文件夹拷贝到指定地址,在目的地址创建一个相同的文件夹
    20. File file=new File(dest, entry.toString());//因为entry为zip类型,所以需要转换成字符串
    21. file.mkdirs();
    22. }else {
    23. //文件
    24. //需要读取压缩包的文件,将其写到指定目的地址
    25. FileOutputStream fo=new FileOutputStream(new File(dest,entry.toString()));
    26. int b;
    27. while((b=zip.read())!=-1) {
    28. fo.write(b);
    29. }
    30. fo.close();
    31. //关闭zip,证明一个文件已经处理完毕
    32. zip.closeEntry();
    33. }
    34. }
    35. zip.close();
    36. }

    压缩流

    压缩包里面的每一个文件或文件夹,看成一个ZipEntry对象。

    压缩本质:把每一个文件或文件夹看成一个ZipEntry对象,放入到压缩包中。

    练习1:压缩文件

    将“D:\Date\c.txt”压缩到“D:\Date”

    1. public class test {
    2. public static void main(String [] args) throws IOException {
    3. //压缩文件
    4. File src=new File("D:\\Date\\c.txt");//要压缩的文件地址
    5. File dest=new File("D:\\Date");//压缩包的文件地址
    6. onZip(src, dest);
    7. }
    8. //创建一个压缩文件的方法
    9. public static void onZip(File src,File dest) throws IOException {
    10. //创建一个压缩流来关联压缩包
    11. ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(new File(dest,"c.zip")));//在根目录下创建一个zip的压缩包(一个壳子)
    12. //创建ZipEntry对象,路径为压缩包里面的路径
    13. ZipEntry entry=new ZipEntry("c.txt");
    14. //把entry对象放到压缩包中
    15. zos.putNextEntry(entry);
    16. //将c.txt的内容写到压缩包中
    17. FileInputStream fi=new FileInputStream(src);//读
    18. int b;
    19. while((b=fi.read())!=-1) {
    20. zos.write(b);
    21. }
    22. zos.closeEntry();
    23. zos.close();
    24. }
    25. }

    练习2:压缩文件夹

    将“aaa”文件夹压缩成一个“aaa.zip”压缩包

    1. public class test {
    2. public static void main(String [] args) throws IOException {
    3. //压缩文件夹
    4. File src=new File("D:\\Date\\aaa");//文件夹的路径
    5. //防止若改变了文件夹的路径,压缩包的路径找不到,所以将压缩包的路径拆开书写
    6. //压缩包的父级路径
    7. File parent=src.getParentFile();
    8. //创建压缩包路径
    9. File dest=new File(parent,src.getName()+".zip");
    10. //创建压缩流关联压缩包
    11. ZipOutputStream zos=new ZipOutputStream( new FileOutputStream(dest));
    12. //获取src内的每一个文件,变成ZipEntry对象中,放入压缩包中
    13. toZip(src, zos, src.getName());
    14. zos.close();
    15. }
    16. //原文件夹
    17. //压缩流
    18. //文件夹的名字,例如是src的aaa文件夹名
    19. public static void toZip(File src,ZipOutputStream zos,String name) throws IOException {
    20. //先遍历src获取每一个文件或文件夹
    21. File[] files=src.listFiles();
    22. for(File file:files) {
    23. //判断文件还是文件夹
    24. if(file.isFile()) {
    25. //文件
    26. //先将文件的路径写到ZipEntry对象中
    27. //file为为文件,file.getname是获取它的文件名为:dsr.txt
    28. //例如:要写入压缩包的路径为:\aaa\dsr.txt(文件夹下的文件)
    29. ZipEntry entry=new ZipEntry(name+"\\"+ file.getName());
    30. zos.putNextEntry(entry);
    31. //再获取file文件中的内容,写入到压缩包中
    32. FileInputStream fi=new FileInputStream(file);
    33. int b;
    34. while((b=fi.read())!=-1) {
    35. zos.write(b);
    36. }
    37. fi.close();
    38. zos.closeEntry();
    39. }else {
    40. //文件夹
    41. //递归调用方法
    42. toZip(file,zos,name+"\\"+file.getName());
    43. }
    44. }
    45. }
    46. }

    Commons-io(工具包)

    Commons-io是一组有关IO操作的开源工具包。

    作用:提高IO流的开发效率。

    这是一个第三方工具类。

    copyDirectory()——是将文件夹里的内容复制到另一个文件夹里。

    copyDirectoryToDirectory()——是将第一个文件夹整体复制到另一个文件夹中。

    1. public class test1 {
    2. public static void main(String[] args) throws IOException {
    3. File src= new File("a.txt");
    4. File dest=new File("c.txt");
    5. FileUtils.copyFile(src,dest);
    6. }
    7. }

    都为静态方法之间调用使用。

    Hutool(工具包)

    IO的综合练习

    练习1:制造假数据

    需求:制造假数据也是开发中的一个能力,在各个网上爬取数据,是其中一个方法。
    分析:先将各个网址定义出来,再定义一个方法进行网络爬取

    只演示一个将姓氏爬取出来:

    1. public class test {
    2. public static void main(String [] args) throws IOException {
    3. //https://www.threetong.com/qiming/baobao/27260.html 男生名字
    4. //https://www.threetong.com/qiming/baobao/27261.html 女生名字
    5. //https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d 百家姓
    6. String firstname="https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d";
    7. String boyname="https://www.threetong.com/qiming/baobao/27260.html";
    8. String girlString="https://www.threetong.com/qiming/baobao/27261.html";
    9. //定义一个方法进行爬取
    10. String fn=copy(firstname);
    11. String girl=copy(girlString);
    12. String boy=copy(boyname);
    13. //System.out.println(fn);
    14. //定义一个正则表达式将需要的数据爬取出来
    15. //定义一个方法
    16. ArrayList first=getDate(fn,"(\\W{4})(,|。)",1);
    17. System.out.println(first);
    18. }
    19. private static ArrayList getDate(String str,String regex,int index){//爬取的网址,正则表达式,
    20. //将数据放在集合中,以便后续使用
    21. ArrayList list=new ArrayList<>();
    22. //按正则表达式的规则获取数据
    23. Pattern p=Pattern.compile(regex);//获取正则表达式
    24. //到str中获取数据
    25. Matcher m=p.matcher(str);
    26. while (m.find()) {
    27. list.add(m.group(index));
    28. }
    29. return list;
    30. }
    31. public static String copy(String net) throws IOException {
    32. //定义sb来拼接爬取到的数据
    33. StringBuilder sb=new StringBuilder();
    34. //创建一个URL对象
    35. URL url=new URL(net);
    36. //要链接这个网址
    37. URLConnection con=url.openConnection();
    38. //利用输入流来读取数据
    39. //因为con方法调用只有字节输入流,所以需要将字节输入流包装成字符输入流,因为有文字需要爬取
    40. InputStreamReader isr=new InputStreamReader(con.getInputStream());
    41. int b;
    42. while((b=isr.read())!=-1) {
    43. sb.append((char)b);//将数据拼接到容器中
    44. }
    45. isr.close();
    46. return sb.toString();
    47. }
    48. }

    练习2:带权重的随机点名器

    txt文件中事先准备好10个学生姓名,每个学生的名字独占一行。
    要求1:每次被点到的学生,再次被点到的概率在原先的基础上降低一半。
    本题需要用到集合,I0,权重随机算法

    分析:1)把文件的学生信息读到内存,放入集合中,集合为Student类型。

    2)计算权重总和,利用遍历,再计算每一个的人实际占比,再计算每个人的权重占比范围

    3)随机抽取,获取一个在0~1的随机数,获取随机数在数组的位置。

    4)将这个元素的权重除2,再放回到集合中

    5)再将修改了的值写入文件中

    1. public class Student {
    2. private String name;
    3. private String gender;
    4. private int age;
    5. private double weight;
    6. public Student() {
    7. }
    8. public Student(String name,String gender,int age,double weight) {
    9. this.name=name;
    10. this.gender=gender;
    11. this.age=age;
    12. this.weight=weight;
    13. }
    14. public String getName() {
    15. return name;
    16. }
    17. public void setName(String name) {
    18. this.name = name;
    19. }
    20. public String getGender() {
    21. return gender;
    22. }
    23. public void setGender(String gender) {
    24. this.gender = gender;
    25. }
    26. public int getAge() {
    27. return age;
    28. }
    29. public void setAge(int age) {
    30. this.age = age;
    31. }
    32. public double getWeight() {
    33. return weight;
    34. }
    35. public void setWeight(double weight) {
    36. this.weight = weight;
    37. }
    38. @Override
    39. public String toString() {
    40. return name+"-"+gender+"-"+age+"-"+weight;
    41. }
    42. }
    1. public class test {
    2. public static void main(String [] args) throws IOException {
    3. //将文件的内容读取到内存
    4. //然后放入集合中
    5. ArrayListlist=new ArrayList<>();
    6. BufferedReader bf=new BufferedReader(new FileReader("n.txt"));
    7. String len;
    8. while((len=bf.readLine())!=null) {
    9. String[] arr=len.split("-");
    10. Student stu=new Student(arr[0],arr[1],Integer.parseInt(arr[2]),Double.parseDouble(arr[3]));
    11. list.add(stu);
    12. }
    13. bf.close();
    14. //计算权重总和
    15. double weight=0;
    16. for(Student s:list) {//遍历集合
    17. weight+=s.getWeight();
    18. }
    19. //计算每一个人的权重占比,放入数组中
    20. Double[] arr=new Double[list.size()];
    21. int index=0;
    22. for(Student stu:list) {
    23. arr[index]=stu.getWeight()/weight;
    24. index++;
    25. }
    26. //计算每个人权重占比范围
    27. for(int i=1;i
    28. arr[i]=arr[i]+arr[i-1];
    29. }
    30. System.out.println(Arrays.toString(arr));
    31. //获取一个随机数在0~1之间
    32. Double d= Math.random();
    33. System.out.println(d);
    34. //查找随机数在这个数组中的位置
    35. //利用二分查找
    36. int num= Arrays.binarySearch(arr, d);
    37. int result=-num-1;//将插入点转为正数
    38. Student s1= list.get(result);
    39. System.out.println(s1);
    40. //将这个元素的权重值要除2
    41. double w= s1.getWeight()/2;
    42. s1.setWeight(w);
    43. //将修改了的值再次写入到文件中
    44. BufferedWriter bw=new BufferedWriter(new FileWriter("n.txt"));
    45. for(Student s:list) {
    46. bw.write(s.toString());
    47. bw.newLine();
    48. }
    49. bw.close();
    50. }
    51. }

    练习3:登陆小案例(添加锁定账号功能)

    将正确的用户名和密码手动保存在本地的文件中。格式为:username=zhangsan&password=123&count=0让用户键盘录入用户名和密码
    比较用户录入的和正确的用户名密码是否一致
    如果一致则打印登陆成功
    如果不一致则打印登陆失败,连续输错三次被锁定。

    1. public class test {
    2. public static void main(String [] args) throws IOException {
    3. //先读取文件
    4. BufferedReader br=new BufferedReader(new FileReader("n.txt"));
    5. String len=br.readLine();
    6. br.close();
    7. //将这个字符串分隔开
    8. String[] arr=len.split("&");
    9. String arr1=arr[0];//用户名
    10. String arr2=arr[1];//密码
    11. String arr3=arr[2];//次数
    12. //再将这三个分割
    13. String[] str1=arr1.split("=");
    14. String name=str1[1];//用户名
    15. String[] str2=arr2.split("=");
    16. String passwd=str2[1];//密码
    17. String[] str3=arr3.split("=");
    18. int count=Integer.parseInt(str3[1]);//次数
    19. //从键盘输入
    20. Scanner sc=new Scanner(System.in);
    21. while(true) {
    22. System.out.println("请输入用户名");
    23. String n=sc.nextLine();
    24. System.out.println("请输入密码");
    25. String number=sc.nextLine();
    26. //与用户比较
    27. if(n.equals(name)&&number.equals(passwd)&&count<=3) {
    28. //一致
    29. System.out.println("登录成功");
    30. break;
    31. }else {
    32. //不一致
    33. System.out.println("登录失败");
    34. count++;
    35. if(count<3) {
    36. System.out.println("登录失败,还剩"+(3-count)+"机会");
    37. }else {
    38. System.out.println("账户被锁定");
    39. break;
    40. }
    41. }
    42. write("username="+name+" "+"passwd="+passwd+" "+"count="+count);
    43. }
    44. }
    45. //要将count写入到文件中
    46. public static void write(String str) throws IOException {
    47. BufferedWriter bw=new BufferedWriter(new FileWriter("t.txt"));
    48. bw.write(str);
    49. bw.close();
    50. }
    51. }

    将读n文件的信息,写入到t文件中。

  • 相关阅读:
    低代码搭建高效管理房屋检测系统案例分析
    Docker build创建指定容器镜像
    c++之旅第七弹——继承
    贴近摄影测量 | 中国最神秘的建筑!
    Nginx日志功能介绍
    机器学习(17)---支持向量机(SVM)
    linux安装、及JDK配置环境变量
    pyinstaller 使用
    Windows 堆管理机制 [3] Windows XP SP2 – Windows 2003 版本
    经纬恒润汽车电子研发新成果亮相重庆智博会
  • 原文地址:https://blog.csdn.net/m0_70851493/article/details/139402062