• 【Java基础】IO案例,集合到文件数据排序、复制单级和多级文件夹及复制文件的异常处理


    目录

    一、集合到文件数据排序

    二、复制单级文件夹

    三、复制多级文件夹

    四、复制文件的异常处理

    基本做法:

    JDK7版本改进:

    JDK9版本改进:


    一、集合到文件数据排序

    需求:

    键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),要求按照成绩总分从高到低写入文本文件

    格式:姓名,语文成绩,数学成绩,英语成绩 举例:小玲,98,97100

    分析步骤:

    ○ 定义学生类

    ○ 创建TreeSet集合,通过比较器进行排序

    ○ 键盘录入学生数据

    ○ 创建学生对象,把键盘录入的数据对应赋值给学生对象的成员变量

    ○ 把学生对象添加到TreeSet集合

    ○ 创建字符缓冲输出流对象

    ○ 遍历集合,得到每一个学生对象

    ○ 把学生对象的数据拼接指定格式的字符串

    ○ 调用字符缓冲输出流对象的方法写数据

    ○ 释放资源

    代码实现:

    学生类

    1. public class Student {
    2. // 姓名
    3. private String name;
    4. // 语文成绩
    5. private int chinese;
    6. // 数学成绩
    7. private int math;
    8. // 英语成绩
    9. private int english;
    10. public Student() {
    11. super();
    12. }
    13. public Student(String name, int chinese, int math, int english) {
    14. super();
    15. this.name = name;
    16. this.chinese = chinese;
    17. this.math = math;
    18. this.english = english;
    19. }
    20. public String getName() {
    21. return name;
    22. }
    23. public void setName(String name) {
    24. this.name = name;
    25. }
    26. public int getChinese() {
    27. return chinese;
    28. }
    29. public void setChinese(int chinese) {
    30. this.chinese = chinese;
    31. }
    32. public int getMath() {
    33. return math;
    34. }
    35. public void setMath(int math) {
    36. this.math = math;
    37. }
    38. public int getEnglish() {
    39. return english;
    40. }
    41. public void setEnglish(int english) {
    42. this.english = english;
    43. }
    44. public int getSum() {
    45. return this.chinese + this.math + this.english;
    46. }
    47. }

    测试 类:

    1. public class TreeSetToFileDemo {
    2. public static void main(String[] args) throws IOException {
    3. //创建TreeSet集合,通过比较器排序进行排序
    4. TreeSet ts = new TreeSet(new Comparator() {
    5. @Override
    6. public int compare(Student s1, Student s2) {
    7. //成绩总分从高到低
    8. int num = s2.getSum() - s1.getSum();
    9. //次要条件
    10. int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
    11. int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;
    12. int num4 = num3 == 0 ? s1.getName().compareTo(s2.getName()) :
    13. num3;
    14. return num4;
    15. }
    16. });
    17. //键盘录入学生数据
    18. for (int i = 0; i < 5; i++) {
    19. Scanner sc = new Scanner(System.in);
    20. System.out.println("请录入第" + (i + 1) + "个学生信息:");
    21. System.out.println("姓名:");
    22. String name = sc.nextLine();
    23. System.out.println("语文成绩:");
    24. int chinese = sc.nextInt();
    25. System.out.println("数学成绩:");
    26. int math = sc.nextInt();
    27. System.out.println("英语成绩:");
    28. int english = sc.nextInt();
    29. //创建学生对象,把键盘录入的数据对应赋值给学生对象的成员变量
    30. Student s = new Student();
    31. s.setName(name);
    32. s.setChinese(chinese);
    33. s.setMath(math);
    34. s.setEnglish(english);
    35. //把学生对象添加到TreeSet集合
    36. ts.add(s);
    37. }
    38. //创建字符缓冲输出流对象
    39. BufferedWriter bw = new BufferedWriter(new
    40. FileWriter("myCharStream\\ts.txt"));
    41. //遍历集合,得到每一个学生对象
    42. for (Student s : ts) {
    43. //把学生对象的数据拼接成指定格式的字符串
    44. //格式:姓名,语文成绩,数学成绩,英语成绩
    45. StringBuilder sb = new StringBuilder();
    46. sb.append(s.getName()).append(",").append(s.getChinese()).append(",")
    47. .append(s.getMath()).append(",").append(s.getEnglish()).append(",").append(s.getSum());
    48. // 调用字符缓冲输出流对象的方法写数据
    49. bw.write(sb.toString());
    50. bw.newLine();
    51. bw.flush();
    52. }
    53. //释放资源
    54. bw.close();
    55. }
    56. }

    二、复制单级文件夹

    需求:

    把“E:\itcast”这个文件夹复制到模块目录下

    步骤分析:

    1、创建数据源目录File对象,路径是E:\itcast

    2、获取数据源目录File对象的名称

    3、创建目的地目录File对象,路径由(模块名+第2步获取的名称)组成

    4、判断第3步创建的File是否存在,如果不存在,就创建

    5、获取数据源目录下所有文件的File数组

    6、遍历File数组,得到每一个File对象,该File对象,其实就是数据源文件

    7、获取数据源文件File对象的名称

    8、创建目的地文件File对象,路径由于(目的地目录+第7步获取的名称)组成

    9、复制文件

    由于不清楚数据源目录下的文件都是什么类型的,所以采用字节流复制文件

    采用参数为File的构造方法

    代码实现:

    1. public class CopyFolderDemo {
    2. public static void main(String[] args) throws IOException {
    3. //创建数据源目录File对象,路径是E:\\itcast
    4. File srcFolder = new File("E:\\itcast");
    5. //获取数据源目录File对象的名称(itcast)
    6. String srcFolderName = srcFolder.getName();
    7. //创建目的地目录File对象,路径名是模块名+itcast组成(myCharStream\\itcast)
    8. File destFolder = new File("myCharStream",srcFolderName);
    9. //判断目的地目录对应的File是否存在,如果不存在,就创建
    10. if(!destFolder.exists()) {
    11. destFolder.mkdir();
    12. }
    13. //获取数据源目录下所有文件的File数组
    14. File[] listFiles = srcFolder.listFiles();
    15. //遍历File数组,得到每一个File对象,该File对象,其实就是数据源文件
    16. for(File srcFile : listFiles) {
    17. //数据源文件:E:\\itcast\\mn.jpg
    18. //获取数据源文件File对象的名称(mn.jpg)
    19. String srcFileName = srcFile.getName();
    20. //创建目的地文件File对象,路径名是目的地目录+mn.jpg组成
    21. //(myCharStream\\itcast\\mn.jpg)
    22. File destFile = new File(destFolder,srcFileName);
    23. //复制文件
    24. copyFile(srcFile,destFile);
    25. }
    26. }
    27. private static void copyFile(File srcFile, File destFile) throws IOException {
    28. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
    29. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
    30. byte[] bys = new byte[1024];
    31. int len;
    32. while ((len=bis.read(bys))!=-1) {
    33. bos.write(bys,0,len);
    34. }
    35. bos.close();
    36. bis.close();
    37. }
    38. }

    三、复制多级文件夹

    需求:

    把“E:\itcast”这个文件夹复制到 F盘目录下

    步骤分析:

    1、创建数据源File对象,路径是E:\itcast

    2、创建目的地File对象,路径是F:\

    3、写方法实现文件夹的复制,参数为数据源File对象和目的地File对象

    4、判断数据源File是否是文件

    是文件:直接复制,用字节流

    不是文件:在目的地下创建该目录,遍历获取该目录下所有文件的File数组,得到每一个File对象

    回到3继续(递归)

    代码实现:

    1. public class CopyFoldersDemo {
    2. public static void main(String[] args) throws IOException {
    3. //创建数据源File对象,路径是E:\\itcast
    4. File srcFile = new File("E:\\itcast");
    5. //创建目的地File对象,路径是F:\\
    6. File destFile = new File("F:\\");
    7. //写方法实现文件夹的复制,参数为数据源File对象和目的地File对象
    8. copyFolder(srcFile,destFile);
    9. }
    10. //复制文件夹
    11. private static void copyFolder(File srcFile, File destFile) throws IOException {
    12. //判断数据源File是否是目录
    13. if(srcFile.isDirectory()) {
    14. //在目的地下创建和数据源File名称一样的目录
    15. String srcFileName = srcFile.getName();
    16. File newFolder = new File(destFile,srcFileName); //F:\\itcast
    17. if(!newFolder.exists()) {
    18. newFolder.mkdir();
    19. }
    20. //获取数据源File下所有文件或者目录的File数组
    21. File[] fileArray = srcFile.listFiles();
    22. //遍历该File数组,得到每一个File对象
    23. for(File file : fileArray) {
    24. //把该File作为数据源File对象,递归调用复制文件夹的方法
    25. copyFolder(file,newFolder);
    26. }
    27. } else {
    28. //说明是文件,直接复制,用字节流
    29. File newFile = new File(destFile,srcFile.getName());
    30. copyFile(srcFile,newFile);
    31. }
    32. }
    33. //字节缓冲流复制文件
    34. private static void copyFile(File srcFile, File destFile) throws IOException {
    35. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
    36. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
    37. byte[] bys = new byte[1024];
    38. int len;
    39. while ((len = bis.read(bys)) != -1) {
    40. bos.write(bys, 0, len);
    41. }
    42. bos.close();
    43. bis.close();
    44. }
    45. }

    四、复制文件的异常处理

    基本做法:

    1. public class CopyFileDemo {
    2. public static void main(String[] args) {
    3. }
    4. //try...catch...finally
    5. private static void method2() {
    6. FileReader fr = null;
    7. FileWriter fw = null;
    8. try {
    9. fr = new FileReader("fr.txt");
    10. fw = new FileWriter("fw.txt");
    11. char[] chs = new char[1024];
    12. int len;
    13. while ((len = fr.read()) != -1) {
    14. fw.write(chs, 0, len);
    15. }
    16. } catch (IOException e) {
    17. e.printStackTrace();
    18. } finally {
    19. if(fw!=null) {
    20. try {
    21. fw.close();
    22. } catch (IOException e) {
    23. e.printStackTrace();
    24. }
    25. }
    26. if(fr!=null) {
    27. try {
    28. fr.close();
    29. } catch (IOException e) {
    30. e.printStackTrace();
    31. }
    32. }
    33. }
    34. }
    35. //抛出处理
    36. private static void method1() throws IOException {
    37. FileReader fr = new FileReader("fr.txt");
    38. FileWriter fw = new FileWriter("fw.txt");
    39. char[] chs = new char[1024];
    40. int len;
    41. while ((len = fr.read()) != -1) {
    42. fw.write(chs, 0, len);
    43. }
    44. fw.close();
    45. fr.close();
    46. }
    47. }

    JDK7版本改进:

    1. public class CopyFileDemo {
    2. public static void main(String[] args) {
    3. }
    4. //JDK7的改进方案
    5. private static void method3() {
    6. try(FileReader fr = new FileReader("fr.txt");
    7. FileWriter fw = new FileWriter("fw.txt");){
    8. char[] chs = new char[1024];
    9. int len;
    10. while ((len = fr.read()) != -1) {
    11. fw.write(chs, 0, len);
    12. }
    13. } catch (IOException e) {
    14. e.printStackTrace();
    15. }
    16. }
    17. }

    JDK9版本改进:

    1. public class CopyFileDemo {
    2. public static void main(String[] args) {
    3. }
    4. //JDK9的改进方案
    5. private static void method4() throws IOException {
    6. FileReader fr = new FileReader("fr.txt");
    7. FileWriter fw = new FileWriter("fw.txt");
    8. try(fr;fw){
    9. char[] chs = new char[1024];
    10. int len;
    11. while ((len = fr.read()) != -1) {
    12. fw.write(chs, 0, len);
    13. }
    14. } catch (IOException e) {
    15. e.printStackTrace();
    16. }
    17. }
    18. }
  • 相关阅读:
    Sentinel —实时监控
    线段树入门+例题详解
    力扣 26. 删除有序数组中的重复项
    RHCE---Linux的计划任务
    干掉Switch-Case、If-Else----订阅发布模式+事件驱动模式
    初识c++
    Google Earth Engine(GEE)—— 快速进行农田作物土地分类和面积统计
    技术路线决定出海方向:中国车企的全球市场密码
    随机过程理论知识(六)
    ...mapState 和 ...mapMutations入门使用
  • 原文地址:https://blog.csdn.net/m0_61961937/article/details/126831945