IO流作业

练习-:字节输出流写出字节数据

1.描述:利用字节输出流一次写一个字节的方式,向D盘的a.txt文件输出字符‘a’。

操作步骤:

1.创建字节输出流FileOutputStream对象并指定文件路径.

2.调用字节输出流的write(int byte)方法写出数据

代码:

 1 package com.heima.zuoye;
 2 
 3 import java.io.FileOutputStream;
 4 
 5 public class Demo01 {
 6     public static void main(String[] args) throws Exception{
 7         //创建字节输出流FileOutputStream对象并指定文件路径
 8         FileOutputStream fileOutputStream = new FileOutputStream("D:\\a.txt");
 9         //调用对象
10         fileOutputStream.write(97);
11         //关闭流
12         fileOutputStream.close();
13     }
14 }

练习二:字节输出流写出字节数组数据.

描述:利用字节输出流一次写一个字节数组的方式向D盘的b.txt文件输出内容:"i love java"。

操作步骤:

1.创建字节输出流FileOutputStream对象并指定文件路径。

2.调用字节输出流的write(byte[] buf)方法写出数据.

代码;

 1 package com.heima.zuoye;
 2 
 3 import java.io.FileOutputStream;
 4 
 5 public class Demo02 {
 6     public static void main(String[] args) throws Exception{
 7         //创建字节输出流对象并指定文件
 8         FileOutputStream fileOutputStream = new FileOutputStream("D:\\b.txt");
 9         //调用字节输出流的write(byte[]buf)方法写出数据
10         byte[] bytes = "I LOVE JAVA".getBytes();
11         fileOutputStream.write(bytes);
12         fileOutputStream.close();
13     }
14 }

 

练习三:文件的续写和换行输出

描述:在D盘下,有一c.txt 文件中内容为:HelloWorld 

在c.txt文件原内容基础上,添加五句 I love java,而且要实现一句一行操作(注:原文不可覆盖)。

利用字节输出流对象往C盘下c.txt文件输出5句:”i love java”

操作步骤:

  1. 利用两个参数的构造方法创建字节输出流对象,参数一指定文件路径,参数二指定为true

           调用字节输出流的write()方法写入数据,在每一行后面加上换行符:”\r\n”

代码:

 1 package com.heima.zuoye;
 2 
 3 import java.io.FileOutputStream;
 4 
 5 public class Demo03 {
 6     public static void main(String[] args) throws Exception{
 7         //创建字节输出流对象并指定文件路径
 8         FileOutputStream fileOutputStream = new FileOutputStream("D:\\LULIANGHM\\c.txt",true);
 9         //要输出的字符串
10         String content="I LOVE YOU";
11         for (int i = 0; i < 5; i++) {
12             System.out.println(content);
13             fileOutputStream.write(content.getBytes());
14         }
15         //关闭释放资源
16         fileOutputStream.close();
17     }
18 }

 

 

字符输出流写出字符数据并存到集合中

需求说明:从控制台接收3名学员的信息,每条信息存储到一个Student对象中,将多个Student对象存储到一个集合中。输入完毕后,将所有学员信息存储到文件Student.txt中。每名学员信息存储一行,多个属性值中间用逗号隔开。

         操作步骤:

  1. 创建Student类,有如下属性:

学号、姓名、性别、年龄

全部属性使用String类型。要求有无参,全参构造方法。所有属性私有,并提供公有get/set方法。

  1. 创建MainApp类,包含main()方法
  2. 在main()方法中:

1)         定义一个存储Student对象的集合;

2)         循环3次,从控制台接收3名学员信息,每条信息封装一个Student对象,将每个Student对象存储到集合中。

3)         遍历集合,获取每个Student对象,取出所有属性值,输出到文件Test2_2.txt中。每名学员信息占一行。

代码:

 1 public class Dome04 {
 2     public static void main(String[] args) throws IOException {
 3         // 1.定义学生类, 定义存学生的集合
 4         ArrayList<Student> list = new ArrayList<Student>();
 5         // 2.通过3次循环,完成如下操作
 6         Scanner sc = new Scanner(System.in);
 7         for (int i = 1; i<= 3; i++) {
 8             // 键盘输入学生的信息,
 9             System.out.print("请输入第" + i + "名学生的学号:");
10             String id = sc.next();
11             System.out.print("请输入第" + i + "名学生的姓名:");
12             String name = sc.next();
13             System.out.print("请输入第" + i + "名学生的性别:");
14             String sex = sc.next();
15             System.out.print("请输入第" + i + "名学生的年龄:");
16             String age = sc.next();
17             // 把信息封装到Student对象中
18             Student s = new Student(id, name, sex, age);
19             // 把Student对象存到集合里
20             list.add(s);
21         }
22         // 3.将所有学员信息存储到文件Student.txt中。
23         FileWriter out = new FileWriter("Student.txt");
24         // 每名学员信息存储一行,多个属性值中间用逗号隔开。
25         for (int i = 0; i<list.size(); i++) {
26             // 1.获取集合中每一个学生对象
27             Student s = list.get(i);
28             // 2.获取对象中的每一个属性值,多个属性值中间用逗号隔开
29             String line = s.getId() + "," + s.getName() + "," + s.getSex() + "," + s.getAge();
30             // 3.按照指定的格式把对象的属性值,写入到文件中
31             out.write(line);
32             out.write(System.lineSeparator());
33         }
34         out.close();// 关闭流
35     }
36 }

 

 1 package com.heima.zuoye;
 2 
 3 import java.util.Objects;
 4 
 5 public class Student {
 6     private String name;//定义姓名
 7     private int age;//定义年龄
 8     private String gender;//定义性别
 9     //空参构造方法
10 
11     public Student() {
12     }
13     //满参构造方法
14 
15     public Student(String name, int age, String gender) {
16         this.name = name;
17         this.age = age;
18         this.gender = gender;
19     }
20     //set/get方法
21 
22     public String getName() {
23         return name;
24     }
25 
26     public void setName(String name) {
27         this.name = name;
28     }
29 
30     public int getAge() {
31         return age;
32     }
33 
34     public void setAge(int age) {
35         this.age = age;
36     }
37 
38     public String getGender() {
39         return gender;
40     }
41 
42     public void setGender(String gender) {
43         this.gender = gender;
44     }
45     //重写toString方法
46 
47     @Override
48     public String toString() {
49         return "Student{" +
50                 "name='" + name + '\'' +
51                 ", age=" + age +
52                 ", gender='" + gender + '\'' +
53                 '}';
54     }
55 
56     @Override
57     public boolean equals(Object o) {
58         if (this == o) return true;
59         if (o == null || getClass() != o.getClass()) return false;
60         Student student = (Student) o;
61         return age == student.age &&
62                 Objects.equals(name, student.name) &&
63                 Objects.equals(gender, student.gender);
64     }
65 
66     @Override
67     public int hashCode() {
68 
69         return Objects.hash(name, age, gender);
70     }
71 }

 

转载于:https://www.cnblogs.com/luliang1215/p/10633130.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值