21---character stream related exercises

Exercise 1: Character output stream write character data

  • Ask the user to input information from the console, and the program will store the information in the file Info.txt. You can enter multiple pieces of information, and each piece of information is stored on one line. When the user inputs: "886", the program ends.

Steps:

  1. Create the MainAPP class and include the main() method
  2. Implement the program according to the above requirements

Code:

public class Test01_07 {
    
    
    public static void main(String[]args) throws IOException {
    
    
       //1. 指定输出流, 对应的文件Info.txt
       FileWriter bw= new  FileWriter("Info.txt");
       //2.采用循环的方式,把每条信息存储一行到Info.txt中
       Scanner sc= new Scanner(System.in);
       while(true){
    
    
           //获取键盘输入的一行内容
           System.out.print("请输入内容:");
           String str= sc.nextLine();
           //当用户输入:”886”时,程序结束。
           if ("886".equals(str)) {
    
    
              break;//跳出循环
           }
           //把内容写入到Info.txt文件中
           bw.write(str);
           //换行
           bw.write(System.lineSeparator());
       }
       //关闭流
       bw.close();
    }
}

Exercise 2: Character output stream write character data and store it in the collection

  • Receive the information of 3 students from the console, each information is stored in a Student object, and multiple Student objects are stored in a collection. After inputting, store all student information in the file Student.txt. Each student information is stored in one row, and multiple attribute values ​​are separated by commas.

Steps:
1. Create a Student class with the following attributes:
student ID, name, gender, and age.
All attributes use String type. Requires no parameters, full parameter construction method. All attributes are private, and public get/set methods are provided.
2. Create the MainApp class, including the main() method.
In the main() method:
1. Define a collection of Student objects;
2. Loop 3 times, receive 3 student information from the console, and each information encapsulates a Student object , Store each Student object in the collection.
3. Traverse the collection, get each Student object, take out all the attribute values, and output to the file Test2_2.txt. Each student's information is on one line.

Code:

public class Task02_03 {
    
    
    public static void main(String[] args) throws IOException {
    
    
       // 1.定义学生类, 定义存学生的集合
       ArrayList<Student> list = new ArrayList<Student>();
       // 2.通过3次循环,完成如下操作
       Scanner sc = new Scanner(System.in);
       for (int i = 1; i<= 3; i++) {
    
    
           // 键盘输入学生的信息,
           System.out.print("请输入第" + i + "名学生的学号:");
           String id = sc.next();
           System.out.print("请输入第" + i + "名学生的姓名:");
           String name = sc.next();
           System.out.print("请输入第" + i + "名学生的性别:");
           String sex = sc.next();
           System.out.print("请输入第" + i + "名学生的年龄:");
           String age = sc.next();
           // 把信息封装到Student对象中
           Student s = new Student(id, name, sex, age);
           // 把Student对象存到集合里
           list.add(s);
       }
       // 3.将所有学员信息存储到文件Student.txt中。
       FileWriter out = new FileWriter("Student.txt");
       // 每名学员信息存储一行,多个属性值中间用逗号隔开。
       for (int i = 0; i<list.size(); i++) {
    
    
           // 1.获取集合中每一个学生对象
           Student s = list.get(i);
           // 2.获取对象中的每一个属性值,多个属性值中间用逗号隔开
           String line = s.getId() + "," + s.getName() + "," + s.getSex() + "," + s.getAge();
           // 3.按照指定的格式把对象的属性值,写入到文件中
           out.write(line);
           out.write(System.lineSeparator());
       }
       out.close();// 关闭流
    }
}

Guess you like

Origin blog.csdn.net/qq_44787898/article/details/106965573