--------------------------------------------------------------
练习:
题 1: 有一个文本文件如下, 数字之间空格分隔
将其中的数字实现倒序排列并保存到另一个文本文件中, 一个数字一行
9 5 2 7
6 5 2
结果文件:
- 9
- 7
- 6
- 5
- 5
- 2
- 2
- public static void main(String[] args) throws Exception {
- // 读取数据
- BufferedReader br = new BufferedReader(new FileReader("a.txt"));
- //
- TreeSet<String> set = new TreeSet<>(new Comparator<String>() {
- @Override
- public int compare(String o1, String o2) {
- return o2.compareTo(o1);
- }
- });
- int x = 0;
- //
- String line = null;
- while((line = br.readLine()) != null){
- String[] split = line.split(" ");
- // 放到集合中时, 不能只放数字, 需要让它唯一.
- // 数字_数字
- for (String n : split) {
- set.add(n+"_"+x++);//9_0 9_1
- }
- }
- //
- br.close();
- // 创建输出流
- FileWriter fw = new FileWriter("b.txt");
- // 变量集合, 将元素中的数字写出到文件中
- for(String n : set){
- fw.write(n.split("_")[0]);
- // 换行
- fw.write("\r\n");
- }
- //
- fw.close();
- }
题 2: 一个文本文件的内容如下:
- 001 toms 10
- 002 Tyson 20
- 003 Jerry 12
其中的内容一行表示的是一个学生对象的基本信息, 信息用空格分隔
尝试将其中的文本信息解析成学生对象, 并保存在一个学生集合中
- public static void main(String[] args) throws Exception {
- BufferedReader br = new BufferedReader(new FileReader("stu.txt"));
- //
- ArrayList<Student> list = new ArrayList<Student>();
- String line = null;
- while((line = br.readLine()) != null){
- String[] info = line.split(" ");
- String id = info[0];
- String name = info[1];
- int age = Integer.parseInt(info[2]);
- //
- Student s = new Student(id, name, age);
- list.add(s);
- }
- br.close();
- //
- for (Student stu : list) {
- System.out.println(stu);
- }
- }
练习题:
1. 将集合中的元素保存到文件中
重点:
集合的遍历, 输出流的选择: 由于一般都是文本文件, 并且一个元素占用一行, 所以最好选择缓冲字符流, 可以方便实现换行.
2. 将文件中的数据读取到集合中
重点:
集合的选择! 由于文件中的内容有可能是重复的, 在选择集合的时候, 就不能选择元素是唯一的集合, 那样, 将导致文件内容的丢失!
3. 将两个路径的文件进行汇总!
重点:
汇总的文件输出流必须在所有的文件都合并完之后再关闭, 不能提前关闭!
4. 随机获取文本文件的一行
重点:
文本文件中的内容只能是顺序读取, 而集合中的元素是可以随机的获取的, 所以需要将文件中的行转换成集合中的元素.
5. 复制单级文件夹下的文件
重点:
流的选择: 由于文件的类型多种, 并不固定是文本文件, 所以在流的选择上, 应该使用万能的字节流.
6. 复制多级文件夹下的所有文件
重点:
递归的方法调用!
java 第二 API:commons-io.
https://mvnrepository.com/ -> 互联网上 jar 包的仓库!!! 配合 maven 使用!
7. 复制指定类型文件并改名
重点:
指定类型的文件判断: 对文件名必须转换成统一的大小写再使用 endsWith 方法判断!
改名: 使用 renameTo 方法需要注意必须在同一个文件夹下才是改名!
8. 录入名字字符串并排序至文件
重点:
由于有排序, 考虑使用 TreeSet, 但是注意重复字符串的处理!
以下是课上的代码, 并没有考虑到这个点!
- /*
- * 需求:
- 从键盘录入 5 个人的姓名, 对其排序后存入文本文件
- */
- public class SortNameDemo {
- public static void main(String[] args) throws Exception {
- Scanner sc = new Scanner(System.in);
- // 创建集合, 用于对名字排序
- TreeSet<String> set = new TreeSet<>();
- // 遍历输入元素
- for(int i = 1;i<=5;i++){
- System.out.println("输入第" + i + "个人的姓名:");
- set.add(sc.nextLine());
- }
- BufferedWriter bw = new BufferedWriter(new FileWriter("name.txt"));
- // 遍历集合, 将元素存入文件
- for (String name : set) {
- bw.write(name);
- bw.newLine();
- }
- //
- bw.close();
- }
- }
9. 将无序字符排序后保存至文件
10. 修改文本文件的内容
重点:
对同一个文件先开输入流, 再开输出流. 不要交叉!!!
11. 统计各个字符的个数
使用 IO 模拟学生管理系统:
/*
* 题意:
使用文件保存多个学生信息, 在程序中提供查询, 添加, 删除功能
*/
重点: 随时使用流, 随时就能写出来! 并且准确的判断使用字符流还是字节流方便!
- public class StudentManageDemo {
- // 将扫描器提升到成员位置
- static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) throws Exception {
- while (true) {
- System.out.println("1. 查询 \ t2. 添加 \ t3. 删除 \ t4. 修改 \ t5. 退出");
- int op = sc.nextInt();
- if (op == 1) {
- search();
- } else if (op == 2) {
- add();
- } else if (op == 3) {
- delete();
- } else if (op == 4) {
- modify();
- } else {
- System.out.println("ByeBye");
- break;
- }
- }
- }
- // 按学号查询
- private static void search() throws Exception {
- //
- System.out.println("输入要查询的学号:");
- String id = sc.next();
- if (check(id)) {
- // 读取文件, 将对应学号的信息显示:
- BufferedReader br = new BufferedReader(new FileReader("info.txt"));
- String line = null;
- while ((line = br.readLine()) != null) {
- if (line.startsWith(id)) {
- String[] split = line.split("_");
- String num = split[0];
- String name = split[1];
- String mark = split[2];
- System.out.println("学号:\t 姓名:\t 成绩:");
- System.out.println(num + "\t" + name + "\t" + mark);
- break;
- }
- }
- br.close();
- } else {
- System.out.println("此学号不存在!");
- }
- }
- // 删除学生信息
- private static void delete() throws Exception {
- System.out.println("输入要删除的学号:");
- String id = sc.next();
- if (check(id)) {
- BufferedReader br = new BufferedReader(new FileReader("info.txt"));
- ArrayList<String> list = new ArrayList<String>();
- String line = null;
- while ((line = br.readLine()) != null) {
- list.add(line);
- }
- //
- br.close();
- BufferedWriter bw = new BufferedWriter(new FileWriter("info.txt"));
- // 遍历集合, 找到指定的学号对应的信息, 不写回源文件. 其余的都写回源文件
- for (String info : list) {
- if (!info.startsWith(id)) {
- bw.write(info);
- bw.newLine();
- }
- }
- bw.close();
- System.out.println("学号 :" + id + "删除成功");
- } else {
- System.out.println("你输入的学号不存在!!!");
- }
- }
- // 修改指定学号的信息
- private static void modify() throws Exception {
- System.out.println("输入学号:");
- String id = sc.next();
- // 查询此学号是否可用
- if (check(id)) {
- System.out.println("输入新姓名:");
- String name = sc.next();
- System.out.println("输入新成绩:");
- int mark = sc.nextInt();
- // 打开文件, 写入新数据
- BufferedReader br = new BufferedReader(new FileReader("info.txt"));
- ArrayList<String> list = new ArrayList<String>();
- String line = null;
- while ((line = br.readLine()) != null) {
- list.add(line);
- }
- //
- br.close();
- // 遍历集合, 将指定的学号对应的数据进行修改, 再写回
- BufferedWriter bw = new BufferedWriter(new FileWriter("info.txt"));
- for (String info : list) {
- if (info.startsWith(id)) {
- bw.write(id + "_" + name + "_" + mark);
- bw.newLine();
- } else {
- bw.write(info);
- bw.newLine();
- }
- }
- //
- bw.close();
- System.out.println("修改成功!!!");
- } else {
- System.out.println("学号不存在!");
- }
- }
- private static void add() throws Exception {
- System.out.println("输入学号:");
- String id = sc.next();
- // 查询此学号是否可用
- if (!check(id)) {
- System.out.println("输入姓名:");
- String name = sc.next();
- System.out.println("输入成绩:");
- int mark = sc.nextInt();
- // 打开文件, 写入新数据, 追加模式!!!
- FileWriter fw = new FileWriter("info.txt", true);
- fw.write(id + "_" + name + "_" + mark);
- fw.write("\r\n");
- fw.close();
- } else {
- System.out.println("学号已经存在!");
- }
- }
- // 查询学号是否可用
- // true : 说明已经存在
- // false : 说明不存在, 可用!
- private static boolean check(String id) throws Exception {
- // 读取文件, 拿到所有的学号, 比对,
- BufferedReader br = new BufferedReader(new FileReader("info.txt"));
- String line = null;
- while ((line = br.readLine()) != null) {
- if (line.split("_")[0].equals(id)) {
- return true;
- }
- }
- br.close();
- return false;
- }
- }
来源: http://www.bubuko.com/infodetail-2868084.html