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

作者:小虚竹and掘金 时间:2021-11-08 05:24:51 

一、题目描述

题目:有五个学生,每个学生有 3 门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),

把这些数据存放在磁盘文件 "stud.txt "中。

二、解题思路

1、写学生键盘输入和成绩键盘输入,Scanner input1 = new Scanner(System.in);

2、把学生和成绩拼接成字符串

3、把字符串保存到硬盘文件 "stud.txt "中。

三、代码详解

public class Basics102 {
   public static void  fileWriter(String str)
   {
       FileWriter fw =null;
       try {
            fw = new FileWriter("D:\\stud.txt", true);
           //如["\\stud.txt"]则表示在项目盘符的根目录建立文件,如项目在F盘,则在F盘根目录建立文件
           //如["save\\stud.txt"]则表示在当前项目文件夹里找到命名为[save]的文件夹,把文件新建在该文件夹内
           System.out.println("数据已成功写入");
           fw.write(str);
           fw.close();
       } catch (Exception e) {
           //抛出一个运行时异常(直接停止掉程序)
           throw new RuntimeException("运行时异常",e);
       }finally {
           try {
               //操作完要回收流
               fw.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   }

public static void main(String[] args) {

int i,j,k=1;
       String id[] = new String[5];
       int score[] = new int[15];
       String name[] =  new String[5];
       String str=null;

Scanner inputId = new Scanner(System.in);
       Scanner inputName = new Scanner(System.in);
       Scanner inputScore = new Scanner(System.in);

for(i=0;i<5;i++)
       {
           System.out.print("请输入第"+(i+1)+"位同学的学号:");
           id[i]=inputId.nextLine();
           System.out.print("请输入第"+(i+1)+"位同学的姓名:");
           name[i]=inputName.nextLine();
           for(j=i*3;j<i*3+3;j++)
           {
               System.out.print("请输入第"+(i+1)+"位同学的第"+k+++"门成绩:");
               score[j]=inputScore.nextInt();

}
           k=1;
       }

for(i=0;i<5;i++)
       {
           str="学号"+id[i];
           str=str+" "+"姓名"+name[i];

for(j=i*3;j<i*3+3;j++)
               str=str+" "+"第"+k+++"门成绩="+score[j];
           k=1;
           System.out.println();
           fileWriter(str+"\r\n");
       }
   }
}

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

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

解法二:引入Hutool

解题思路

1、写学生键盘输入和成绩键盘输入,Scanner input1 = new Scanner(System.in);

2、把学生和成绩拼接成字符串

3、把字符串保存到硬盘文件 "stud.txt "中。

在上一个解法的基础上,优化了第三步,使用

将String写入文件,UTF-8编码追加模式

FileUtil.appendUtf8String(str,"D:\stud2.txt");

代码详解

public class Basics102_2 {

public static void main(String[] args) {

int i,j,k=1;
       String id[] = new String[5];
       int score[] = new int[15];
       String name[] =  new String[5];
       String str=null;

Scanner inputId = new Scanner(System.in);
       Scanner inputName = new Scanner(System.in);
       Scanner inputScore = new Scanner(System.in);

for(i=0;i<5;i++)
       {
           System.out.print("请输入第"+(i+1)+"位同学的学号:");
           id[i]=inputId.nextLine();
           System.out.print("请输入第"+(i+1)+"位同学的姓名:");
           name[i]=inputName.nextLine();
           for(j=i*3;j<i*3+3;j++)
           {
               System.out.print("请输入第"+(i+1)+"位同学的第"+k+++"门成绩:");
               score[j]=inputScore.nextInt();

}
           k=1;
       }

for(i=0;i<5;i++)
       {
           str="学号"+id[i];
           str=str+" "+"姓名"+name[i];

for(j=i*3;j<i*3+3;j++)
               str=str+" "+"第"+k+++"门成绩="+score[j];
           str +="\n";
           k=1;
           try {
               //不需要关闭文件流,源码已经有了
               FileUtil.appendUtf8String(str,"D:\\stud2.txt");
           }catch (IORuntimeException e){
               //抛出一个运行时异常(直接停止掉程序)
               throw new RuntimeException("运行时异常",e);
           }
       }
   }
}

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

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

来源:https://juejin.cn/post/7160577945684639752

标签:Java,磁盘,文件
0
投稿

猜你喜欢

  • 探讨Java验证码制作(下篇)

    2023-04-04 11:21:42
  • 解决Mybatis-plus和pagehelper依赖冲突的方法示例

    2022-06-28 16:52:59
  • C# 如何在MVC3中取消备用控制器的选择

    2023-02-16 06:48:18
  • C# 基础入门--常量

    2022-01-29 18:54:53
  • java中的interface接口实例详解

    2023-10-12 22:03:10
  • Java 自定义动态数组方式

    2022-08-26 01:38:37
  • Struts中使用validate()输入校验方法详解

    2023-03-02 19:10:41
  • 国内分布式框架Dubbo使用详解

    2022-05-10 13:38:27
  • 使用SpringDataJpa创建中间表

    2023-11-23 18:01:28
  • Spring Boot Thymeleaf实现国际化的方法详解

    2023-11-24 06:01:01
  • 基于Flutter实现多边形和多角星组件

    2023-06-19 06:02:50
  • C#中的DataSet、string、DataTable、对象转换成Json的实现代码

    2021-12-31 14:35:55
  • Android Studio 超级简单的打包生成apk的方法

    2023-08-07 18:57:28
  • java实现一个简单TCPSocket聊天室功能分享

    2022-06-11 20:04:36
  • Android ViewPager实现Banner循环播放

    2022-07-16 06:24:03
  • java输入字符串并将每个字符输出的方法

    2022-10-04 01:25:37
  • Java动态代理模式的深入揭秘

    2023-07-27 23:41:25
  • MyBatis使用动态SQL标签的小陷阱

    2023-09-11 04:42:57
  • springboot表单提交之validator校验

    2023-05-16 23:19:17
  • C#实现FTP传送文件的示例

    2022-06-24 01:15:20
  • asp之家 软件编程 m.aspxhome.com