java成长日记(8)

接之前的实验题目

2 、编程完成下列功能:
1 )首先建立两个文件 myfiel.txt myfile2.txt
2 )从标准设备中输入多名学生信息,如学号,姓名,专业,班级,家庭住
址等,待输入 "bye" 时结束,将输入内容保存在 myfile1.txt 文件中。
3 )再将 myfile1.txt 文件中内容拷贝到 myfile2.txt
首先,我们先建立一个Student_message类,代码如下:
class Student_message {
    private int ID = 220000;
    private String name = "unnamed";
    private String major = "Computer Science";

    private String Class = "null";

    private String address = "河北工业大学";
    public Student_message(){

    }
    public Student_message(int ID , String name , String major , String Class , String address){
        this.ID = ID;
        this.name = name;
        this.major = major;
        this.Class = Class;
        this.address = address;
    }
    public String msg(){
        return ID+" "+name+" "+major+" "+Class+" "+address;
    }
}

之后,我们在主类里实现主要的功能:

public class exper4has2 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请分别输入学号、姓名、专业、班级和地址(用空格隔开,用回车换行输入下一个学生的信息):");
        FileWriter new_file1 = new FileWriter("Ava/experiment/myfiel.txt");
        FileWriter new_file2 = new FileWriter("Ava/experiment/myfile2.txt");
        String message = scanner.nextLine();
        while (!message.equals("bye")){
            String[] tokens = message.split(" "); //用空格隔开
            int ID = Integer.valueOf(tokens[0]);
            String name = tokens[1];
            String major = tokens[2];
            String Class = tokens[3];
            String address = tokens[4];
            Student_message student = new Student_message(ID , name , major , Class , address);
            new_file1.write(student.msg());
            new_file1.write("\n");
            message = scanner.nextLine();
        }
        new_file1.close();
        FileReader old_file = new FileReader("Ava/experiment/myfiel.txt");
        int b;
        while((b = old_file.read()) != -1){
            new_file2.write(b);
        }
        old_file.close();
        new_file2.close();
    }
}

 关于文件的输入输出我在前一篇日记已经提及了,这里我就不多加赘述。我主要讲讲输入的实现方式。首先,我们依据题意确定了循环停止的条件:当输入“bye”时结束循环。之后,我们定义一个String类型的数组tokens,并用空格隔开;这个tokens的第0个元素是ID,第1个元素是name,第2个元素是major,第3个元素是Class,第4个元素是address;之后我们创建一个学生类,将以上信息赋值到student里,然后在文件里写入student.msg()(即学生信息),并写入“\n”来实现换行功能,之后继续输入message。

我们先输入以下示例:

它会生成myfile1.txt文件:

 

并将 myfile1.txt复制到myfile2.txt当中:

完整代码如下:

import java.io.*;
import java.util.Scanner;

class Student_message {
    private int ID = 220000;
    private String name = "unnamed";
    private String major = "Computer Science";

    private String Class = "null";

    private String address = "河北工业大学";
    public Student_message(){

    }
    public Student_message(int ID , String name , String major , String Class , String address){
        this.ID = ID;
        this.name = name;
        this.major = major;
        this.Class = Class;
        this.address = address;
    }
    public String msg(){
        return ID+" "+name+" "+major+" "+Class+" "+address;
    }
}

public class exper4has2 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请分别输入学号、姓名、专业、班级和地址(用空格隔开,用回车换行输入下一个学生的信息):");
        FileWriter new_file1 = new FileWriter("Ava/experiment/myfiel.txt");
        FileWriter new_file2 = new FileWriter("Ava/experiment/myfile2.txt");
        String message = scanner.nextLine();
        while (!message.equals("bye")){
            String[] tokens = message.split(" "); //用空格隔开
            int ID = Integer.valueOf(tokens[0]);
            String name = tokens[1];
            String major = tokens[2];
            String Class = tokens[3];
            String address = tokens[4];
            Student_message student = new Student_message(ID , name , major , Class , address);
            new_file1.write(student.msg());
            new_file1.write("\n");
            message = scanner.nextLine();
        }
        new_file1.close();
        FileReader old_file = new FileReader("Ava/experiment/myfiel.txt");
        int b;
        while((b = old_file.read()) != -1){
            new_file2.write(b);
        }
        old_file.close();
        new_file2.close();
    }
}

 

另外,恭喜梅老板今天收获了人生中的第八个金球奖 !

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值