Java基础学习之IO流应用案例详解
作者:夏志121 时间:2022-09-11 17:04:42
一、点名器
需求:
我有一个文件里面存储了班级同学的姓名,每一个姓名占一行,要求通过程序实现随机点名器
实现步骤:
创建字符缓冲输入流对象
创建ArrayList集合对象
调用字符缓冲流对象的方法读数据
把读取到的字符串数据存储到集合中
释放资源
使用Randam产生一个随机数,随机数的范围在:[0,集合的长度]
把第6步产生的随机数作为索引到ArrayList集合中获取值
把第7步得到的数据输出在控制台
代码实现:
public class CallNameDemo {
public static void main(String[] args) throws IOException {
//创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("myCharStream\\names.txt"));
//创建ArrayList集合对象
ArrayList<String> array = new ArrayList<String>();
//调用字符缓冲输入流对象的方法读数据
String line;
while ((line=br.readLine())!=null) {
//把读取到的字符串数据存储到集合中
array.add(line);
}
//释放资源
br.close();
//使用Random产生一个随机数,随机数的范围在:[0,集合的长度)
Random r = new Random();
int index = r.nextInt(array.size());
//把第6步产生的随机数作为索引到ArrayList集合中获取值
String name = array.get(index);
//把第7步得到的数据输出在控制台
System.out.println("幸运者是:" + name);
}
}
二、集合到文件
需求:
把ArrayList集合中的学生数据写入到文本文件。要求:每一个学生对象的数据作为文件中的一行数据,格式:学号,姓名,年龄,居住地 举例:itheima001,小林,20,青岛
实现步骤:
定义学生类
创建ArrayList集合
创建学生对象
把学生对象添加到集合中
创建字符缓冲输出流对象
遍历集合,得到每一个学生对象
把学生对象的数据拼接成指定格式的字符串
调用字符缓冲输出流对象的方法写数据
释放资源
代码实现:
学生类
public class Student {
private String sid;
private String name;
private int age;
private String address;
public Student() {
}
public Student(String sid, String name, int age, String address) {
this.sid = sid;
this.name = name;
this.age = age;
this.address = address;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
测试类:
public class ArrayListToFileDemo {
public static void main(String[] args) throws IOException {
//创建ArrayList集合
ArrayList<Student> array = new ArrayList<Student>();
//创建学生对象
Student s1 = new Student("itheima001", "小林", 20, "西安");
Student s2 = new Student("itheima002", "小张", 25, "武汉");
Student s3 = new Student("itheima003", "小王", 23, "郑州");
//把学生对象添加到集合中
array.add(s1);
array.add(s2);
array.add(s3);
//创建字符缓冲输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("myCharStream\\students.txt"));
//遍历集合,得到每一个学生对象
for (Student s : array) {
//把学生对象的数据拼接成指定格式的字符串
StringBuilder sb = new StringBuilder();
sb.append(s.getSid()).append(",").append(s.getName()).append(",").append(s.ge tAge()).append(",").append(s.getAddress());
//调用字符缓冲输出流对象的方法写数据
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
//释放资源
bw.close();
}
}
三、文件到集合
需求:
把文本文件中的数据读取到集合中,并遍历集合。要求:文件中每一行数据是一个学生对象的成员变量值,举例:itheima001,小林,23,青岛
实现步骤:
定义学生类
创建字符缓冲输入流对象
创建ArrayList集合对象
调用字符缓冲输入流对象的方法读数据
把读取到的字符串数据用split()进行分隔,得到一个字符串数组
创建学生对象
把字符串数组中的每一个元素取出来对应的赋值给学生对象的成员变量值
把学生对象添加到集合
释放资源
遍历集合
代码实现:
学生类同上
测试类:
public class FileToArrayListDemo {
public static void main(String[] args) throws IOException {
//创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("myCharStream\\students.txt"));
//创建ArrayList集合对象
ArrayList<Student> array = new ArrayList<Student>();
//调用字符缓冲输入流对象的方法读数据
String line;
while ((line = br.readLine()) != null) {
//把读取到的字符串数据用split()进行分割,得到一个字符串数组
String[] strArray = line.split(",");
//创建学生对象
Student s = new Student();
//把字符串数组中的每一个元素取出来对应的赋值给学生对象的成员变量值
//itheima001,林青霞,30,西安
s.setSid(strArray[0]);
s.setName(strArray[1]);
s.setAge(Integer.parseInt(strArray[2]));
s.setAddress(strArray[3]);
//把学生对象添加到集合
array.add(s);
}
//释放资源
br.close();
//遍历集合
for (Student s : array) {
System.out.println(s.getSid() + "," + s.getName() + "," +
s.getAge() + "," + s.getAddress());
}
}
}
来源:https://blog.csdn.net/m0_61961937/article/details/126831426
标签:Java,IO流
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C++中mutable与volatile的深入理解
2023-10-06 14:23:09
Android RecyclerView线性布局详解(1)
2021-09-16 07:20:25
![](https://img.aspxhome.com/file/2023/4/113274_0s.png)
Spring Boot 利用 XML 方式整合 MyBatis
2023-02-09 23:01:18
![](https://img.aspxhome.com/file/2023/0/76310_0s.webp)
Springboot微服务打包Docker镜像流程解析
2022-02-23 19:39:00
Java中joda日期格式化工具的使用示例
2023-03-01 11:49:10
java中的SpringBoot框架
2022-02-10 16:50:05
【IntelliJ IDEA】Maven构建自己的第一个Java后台的方法
2023-09-24 20:11:16
![](https://img.aspxhome.com/file/2023/5/83015_0s.png)
解决feign调用接口不稳定的问题
2022-01-13 19:28:46
C++ boost scoped_ptr智能指针详解
2023-10-04 19:11:45
![](https://img.aspxhome.com/file/2023/7/117767_0s.png)
android线程消息机制之Handler详解
2023-07-28 11:31:52
![](https://img.aspxhome.com/file/2023/1/85961_0s.jpg)
SpringBoot集成Shiro进行权限控制和管理的示例
2022-12-31 19:23:21
探讨:android项目开发 统筹兼顾 需要考虑的因素
2023-08-05 11:21:32
java版十大排序经典算法:完整代码(3)
2021-07-17 05:09:09
![](https://img.aspxhome.com/file/2023/7/77407_0s.gif)
android开发之调用手机的摄像头使用MediaRecorder录像并播放
2021-12-27 12:50:18
java使用静态关键字实现单例模式
2023-11-29 13:37:33
![](https://img.aspxhome.com/file/2023/3/60763_0s.png)
Java基本语法之内部类示例详解
2023-02-06 14:05:32
Java删除二叉搜索树的任意元素的方法详解
2021-10-04 12:27:26
![](https://img.aspxhome.com/file/2023/1/61021_0s.png)
Java Lambda表达式超详细介绍
2021-09-25 03:16:43
![](https://img.aspxhome.com/file/2023/0/85570_0s.png)
Spring Hystrix熔断报警原理图例解析
2021-12-15 16:44:01
![](https://img.aspxhome.com/file/2023/1/102141_0s.png)
Android Vibrator调节震动代码实例
2022-07-06 04:56:46