Java——字符缓冲流练习之集合到文件、文件到集合、点名器
Java——字符缓冲流练习之集合到文件、文件到集合、点名器
- 一、集合到文件(集合中的数据是字符串)
- 二、集合到文件(集合中的数据是学生对象)
- 三、集合到文件(数据排序)
- 四、文件到集合(文件中的数据是字符串)
- 五、文件到集合(文件中的数据是学生对象的值)
- 六、点名器
一、集合到文件(集合中的数据是字符串)
1、需求
把ArrayList集合中的字符串数据写入到文本文件,要求每一个字符串元素作为文件中的一行数据
2、思路
- 创建ArrayList集合
- 往集合里存储字符串元素
- 创建字符缓冲输出流对象
- 遍历集合,得到每一个字符串数据
- 调用字符缓冲输出流对象的方法写数据
- 释放资源
3、代码实现
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;public class Demo1 {public static void main(String[] args) throws IOException {ArrayList<String> array = new ArrayList<String>();array.add("hello");array.add("world");array.add("java");BufferedWriter bw = new BufferedWriter(new FileWriter("ZiFuLiu\\array.txt"));for (String arr:array){bw.write(arr);bw.newLine();bw.flush();}bw.close();}
}
二、集合到文件(集合中的数据是学生对象)
1、需求
把ArrayList集合中的学生数据写入到文本文件,要求每一个学生对象的数据作为文件中的一行数据
格式:学号,姓名,年龄,居住地
2、思路
- 定义学生类
- 创建ArrayList集合
- 创建学生对象
- 把学生对象添加到集合中
- 创建字符缓冲输出流对象
- 遍历集合,得到每一个学生对象
- 将学生对象的数据拼接成指定格式的字符串
- 调用字符缓冲输出流对象的方法写数据
- 释放资源
3、代码实现
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;public class Demo {public static void main(String[] args) throws IOException {ArrayList<Student> array = new ArrayList<Student>();Student s1 = new Student("001", "zlx", 18, "天津");Student s2 = new Student("002", "dfv", 13, "北京");Student s3 = new Student("003", "vgb", 25, "深圳");Student s4 = new Student("004", "huj", 24, "上海");array.add(s1);array.add(s2);array.add(s3);array.add(s4);BufferedWriter bw = new BufferedWriter(new FileWriter("ZiFuLiu\\student.txt"));for (Student s : array) {StringBuilder sb = new StringBuilder();sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());bw.write(sb.toString());bw.newLine();bw.flush();}bw.close();}
}
三、集合到文件(数据排序)
1、需求
键盘录入5个学生信息(姓名、语文成绩、数学成绩、英语成绩),要求按照成绩总分由高到底写入文本文件
格式:姓名,语文成绩,数学成绩,英语成绩
2、思路
- 定义学生类
- 创建TreeSet集合,通过比较器排序进行排序
- 键盘录入学生成绩
- 创建学生对象,把键盘录入的数据对应赋值给学生对象的成员变量
- 把学生对象添加到TreeSet集合
- 创建字符缓冲输出流对象
- 遍历集合,得到每一个学生对象
- 把学生对象的数据拼接成指定格式的字符串
- 调用字符缓冲输出流对象的方法写数据
- 释放资源
3、代码实现
public class Student {private String name;private int chinese;private int math;private int english;public Student() {}public Student(String name, int chinese, int math, int english) {this.name = name;this.chinese = chinese;this.math = math;this.english = english;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getChinese() {return chinese;}public void setChinese(int chinese) {this.chinese = chinese;}public int getMath() {return math;}public void setMath(int math) {this.math = math;}public int getEnglish() {return english;}public void setEnglish(int english) {this.english = english;}public int getSum() {return this.chinese + this.math + this.english;}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;public class Demo {public static void main(String[] args) throws IOException {TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {int num = s2.getSum() - s1.getSum();int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;int num4 = num3 == 0 ? s1.getEnglish() - s2.getEnglish() : num3;return num4;}});for (int i = 0; i < 5; i++) {Scanner sc = new Scanner(System.in);System.out.println("请录入第" + (i + 1) + "名学生的信息:");System.out.println("姓名:");String name = sc.nextLine();System.out.println("语文成绩:");int chinese = sc.nextInt();System.out.println("数学成绩:");int math = sc.nextInt();System.out.println("英语成绩:");int english = sc.nextInt();Student s = new Student();s.setName(name);s.setChinese(chinese);s.setMath(math);s.setEnglish(english);ts.add(s);}BufferedWriter bw = new BufferedWriter(new FileWriter("ZiFuLiu\\student.txt"));for (Student tss : ts) {StringBuilder sb = new StringBuilder();sb.append(tss.getName()).append(",").append(tss.getChinese()).append(",").append(tss.getMath()).append(",").append(tss.getEnglish());bw.write(sb.toString());bw.newLine();bw.flush();}bw.close();}
}
四、文件到集合(文件中的数据是字符串)
1、需求
把文本文件中的数据读取到集合中,并遍历集合,要求文件中每一行为一个集合元素
2、思路
- 创建字符缓冲输入流对象
- 创建ArrayList集合对象
- 调用字符缓冲输入流对象的方法读数据
- 把读取到的字符串数据存储到集合中
- 释放资源
- 遍历集合
3、代码实现
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;public class Demo2 {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("ZiFuLiu\\array.txt"));ArrayList<String> array = new ArrayList<String>();String line;while ((line=br.readLine())!=null){array.add(line);}br.close();for (String s : array){System.out.println(s);}}
}
五、文件到集合(文件中的数据是学生对象的值)
1、需求
把文本文件中的数据读取到集合中,并遍历集合,要求文件中每一行数据是一个学生对象的成员变量值
格式:学号,姓名,年龄,居住地
2、思路
- 定义学生类
- 创建字符缓冲输入流对象
- 创建ArrayList集合
- 调用字符缓冲输入流对象的方法读数据
- 把读取到的字符串数据用split()分割,得到一个字符串数组
- 创建学生对象
- 把字符串数组中的每一个元素取出来对应的赋值给学生对象的成员变量值
- 把学生对象添加到集合中
- 释放资源
- 遍历集合
3、代码实现
import java.io.*;
import java.util.ArrayList;public class Demo2 {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("ZiFuLiu\\student.txt"));ArrayList<Student> array = new ArrayList<Student>();String line;while ((line = br.readLine()) != null) {String[] strArray = line.split(",");Student stu = new Student();stu.setId(strArray[0]);stu.setName(strArray[1]);stu.setAge(Integer.parseInt(strArray[2]));stu.setAddress(strArray[3]);array.add(stu);}br.close();for (Student s : array) {System.out.println(s.getId() + "," + s.getName() + "," + s.getAge() + "," + s.getAddress());}}
}
六、点名器
1、需求
文件中存储了班级同学的姓名,每个姓名占一行,要求程序实现随机点名器
2、思路
- 创建字符缓冲输入流对象
- 创建ArrayList集合对象
- 调用字符缓冲输入流对象的方法读数据
- 把读取到的字符串数据存储到集合中
- 释放资源
- 使用Random产生一个随机数,随机数范围在[0,集合长度]
- 将上一步中产生的随机数作为索引,到ArrayList集合中获取值
- 将上一步得到的数据输出在控制台
3、代码实现
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;public class Demo3 {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("ZiFuLiu\\name.txt"));ArrayList<String> array = new ArrayList<String>();String line;while ((line = br.readLine()) != null) {array.add(line);}Random r = new Random();int index = r.nextInt(array.size());System.out.println(array.get(index));}
}
Java——字符缓冲流练习之集合到文件、文件到集合、点名器相关推荐
- java之影流之主( 流 )第十五天( ----自动关闭的流--字符缓冲流---Properties--序列化-- )
1. JDK1. 7之后, 所有的流都实现了AutoCloseble接口,因此有了自动关闭流的心特性; 作业练习:使用Map集合;以及String 的split方法 "Success is ...
- Java字节缓冲流/字符流/IO流小结
文章目录 字节缓冲流 字节缓冲流构造方法 字符流 为什么会出现字符流 编码表 字符串中的编码解码问题 字符流中的编码解码问题 字符流写数据的5种方式 字符流读数据的2种方式 字符缓冲流 字符缓冲流特有 ...
- Java学习总结:49(字符缓冲流:BufferedReader)
字符缓冲流:BufferedReader 在开始前先让我们来看一段代码 package Project.Study.IOSystem;import java.io.InputStream;public ...
- Java IO流之字符缓冲流
字符流: 1.加入字符缓存流,增强读取功能(readLine) 2.更高效的读取数据 BufferedReader 从字符输入流读取文本,缓冲各个字符,从而实现字符.数组和行的高效读取. FileRe ...
- java 21 - 6 字符缓冲流的特殊方法以及该方法高效复制文件
字符缓冲流的特殊方法: A.BufferedWriter: public void newLine():根据系统来决定换行符 1 private static void write() throws ...
- 字符缓冲流特有功能复制Java文件
案例需求 使用特有功能把模块目录下的ConversionStreamDemo.java 复制到模块目录下的 Copy.java 实现步骤 根据数据源创建字符缓冲输入流对象 根据目的地创建字符缓冲输出流 ...
- Java学习第十六天——字符流读写文件、字符缓冲流拷贝文件
字符流读写文件 字符流读数据-按单个字符读取 创建字符流读文件对象: Reader reader = new FileReader("readme.txt"); 调用方法读取数 ...
- Java IO流之缓冲流:字节缓冲流BufferedOutputStream BufferedInputStream、字符缓冲流BufferedWriter、BufferedReader
文章目录 缓冲流 1.字节缓冲流 构造方法 2.字符缓冲流 构造方法 练习:文章段落排序 缓冲流 缓冲流在基础的字节流和字符流上做功能的增强,能够高效读写的缓冲流,能够转换编码的转换流,能够持久化存储 ...
- Java18-day09【字节缓冲流、字符流、编码表、字符串与字符流中的编码解码问题、字符流读写数据的方式、字符缓冲流、IO流小结】
视频+资料(工程源码.笔记)[链接:https://pan.baidu.com/s/1MdFNUADVSFf-lVw3SJRvtg 提取码:zjxs] Java基础--学习笔记(零起点打开java ...
最新文章
- Elastic Search 介绍和基本概念
- RxSwift之UI控件UISlider与UIStepper扩展的使用
- App设计灵感之十二组精美的健身App设计案例
- 几个简单的正则小例子
- rpc 服务器不可用_RPC和微服务
- 如何判断一个点在任意四边形内
- c语言无法打开源文件stdafx.h,vs2010 中无法打开 源文件 stdafx.h 未定义标识符 “xxx”...
- Python 函数(参数组合)
- AcWing 9. 分组背包问题(分组背包模板)
- Rxjava的背压策略
- ESP32-CAM与Tonny搭建问题。
- 基于C++实现一个支持简单交互绘图小程序
- BOSS管账深度融合钉能力,打破业财数据壁垒实现数据互通
- 仿简书,知乎pc官网顶部导航栏上下滚动效果
- ROS安装:一键解决人生烦恼
- python延迟实现
- Python爬虫之scrapy框架360全网图片爬取
- phoenix的元数据一般存在哪里_【Python基础】hive的元数据存在哪里
- python后台操作炒股软件下载_GitHub - lipq525/stock-1: stock,股票系统。使用python进行开发。...
- 如何解除WORD限制编辑