[JAVA] Java实现学生成绩输出到磁盘文件的方法详解

1664 0
黑夜隐士 2022-11-8 17:18:16 | 显示全部楼层 |阅读模式
目录

    一、题目描述二、解题思路三、代码详解
      解法二:引入Hutool



一、题目描述

题目:有五个学生,每个学生有 3 门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),
把这些数据存放在磁盘文件 "stud.txt "中。

二、解题思路

1、写学生键盘输入和成绩键盘输入,Scanner input1 = new Scanner(System.in);
2、把学生和成绩拼接成字符串
3、把字符串保存到硬盘文件 "stud.txt "中。

三、代码详解
  1. public class Basics102 {
  2.     public static void  fileWriter(String str)
  3.     {
  4.         FileWriter fw =null;
  5.         try {
  6.              fw = new FileWriter("D:\\stud.txt", true);
  7.             //如["\\stud.txt"]则表示在项目盘符的根目录建立文件,如项目在F盘,则在F盘根目录建立文件
  8.             //如["save\\stud.txt"]则表示在当前项目文件夹里找到命名为[save]的文件夹,把文件新建在该文件夹内
  9.             System.out.println("数据已成功写入");
  10.             fw.write(str);
  11.             fw.close();
  12.         } catch (Exception e) {
  13.             //抛出一个运行时异常(直接停止掉程序)
  14.             throw new RuntimeException("运行时异常",e);
  15.         }finally {
  16.             try {
  17.                 //操作完要回收流
  18.                 fw.close();
  19.             } catch (IOException e) {
  20.                 e.printStackTrace();
  21.             }
  22.         }
  23.     }
  24.     public static void main(String[] args) {
  25.         int i,j,k=1;
  26.         String id[] = new String[5];
  27.         int score[] = new int[15];
  28.         String name[] =  new String[5];
  29.         String str=null;
  30.         Scanner inputId = new Scanner(System.in);
  31.         Scanner inputName = new Scanner(System.in);
  32.         Scanner inputScore = new Scanner(System.in);
  33.         for(i=0;i<5;i++)
  34.         {
  35.             System.out.print("请输入第"+(i+1)+"位同学的学号:");
  36.             id[i]=inputId.nextLine();
  37.             System.out.print("请输入第"+(i+1)+"位同学的姓名:");
  38.             name[i]=inputName.nextLine();
  39.             for(j=i*3;j<i*3+3;j++)
  40.             {
  41.                 System.out.print("请输入第"+(i+1)+"位同学的第"+k+++"门成绩:");
  42.                 score[j]=inputScore.nextInt();
  43.             }
  44.             k=1;
  45.         }
  46.         for(i=0;i<5;i++)
  47.         {
  48.             str="学号"+id[i];
  49.             str=str+" "+"姓名"+name[i];
  50.             for(j=i*3;j<i*3+3;j++)
  51.                 str=str+" "+"第"+k+++"门成绩="+score[j];
  52.             k=1;
  53.             System.out.println();
  54.             fileWriter(str+"\r\n");
  55.         }
  56.     }
  57. }
复制代码





解法二:引入Hutool

解题思路
1、写学生键盘输入和成绩键盘输入,Scanner input1 = new Scanner(System.in);
2、把学生和成绩拼接成字符串
3、把字符串保存到硬盘文件 "stud.txt "中。
在上一个解法的基础上,优化了第三步,使用
将String写入文件,UTF-8编码追加模式
FileUtil.appendUtf8String(str,"D:\stud2.txt");
代码详解
  1. public class Basics102_2 {
  2.     public static void main(String[] args) {
  3.         int i,j,k=1;
  4.         String id[] = new String[5];
  5.         int score[] = new int[15];
  6.         String name[] =  new String[5];
  7.         String str=null;
  8.         Scanner inputId = new Scanner(System.in);
  9.         Scanner inputName = new Scanner(System.in);
  10.         Scanner inputScore = new Scanner(System.in);
  11.         for(i=0;i<5;i++)
  12.         {
  13.             System.out.print("请输入第"+(i+1)+"位同学的学号:");
  14.             id[i]=inputId.nextLine();
  15.             System.out.print("请输入第"+(i+1)+"位同学的姓名:");
  16.             name[i]=inputName.nextLine();
  17.             for(j=i*3;j<i*3+3;j++)
  18.             {
  19.                 System.out.print("请输入第"+(i+1)+"位同学的第"+k+++"门成绩:");
  20.                 score[j]=inputScore.nextInt();
  21.             }
  22.             k=1;
  23.         }
  24.         for(i=0;i<5;i++)
  25.         {
  26.             str="学号"+id[i];
  27.             str=str+" "+"姓名"+name[i];
  28.             for(j=i*3;j<i*3+3;j++)
  29.                 str=str+" "+"第"+k+++"门成绩="+score[j];
  30.             str +="\n";
  31.             k=1;
  32.             try {
  33.                 //不需要关闭文件流,源码已经有了
  34.                 FileUtil.appendUtf8String(str,"D:\\stud2.txt");
  35.             }catch (IORuntimeException e){
  36.                 //抛出一个运行时异常(直接停止掉程序)
  37.                 throw new RuntimeException("运行时异常",e);
  38.             }
  39.         }
  40.     }
  41. }
复制代码




到此这篇关于Java实现学生成绩输出到磁盘文件的方法详解的文章就介绍到这了,更多相关Java成绩输出到磁盘文件内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行