http://375940084.blog.51cto.com/2581965/751040

1.创建Student类存储每个学生信息,属性(学号,姓名,出生日期,得分)
2.从c:/test/student.txt文件中读取学生信息。如下:
       学号,姓名,出生日期,得分
       1,张三,1982-1-1,80
       2,李四,1982-11-15,40
       3,王五,1982-2-8,60
       4,赵六,1982-7-5,70
       5,小明,1981-12-21,70
       6,李大嘴,1982-1-3,70
3.使用List存储6名学生的信息。
4.使用集合排序,将学生信息按时得分从高到低排序,得分相同时按照出生日期升序排序。
5.输出排序后的结果到c:/test/result.txt文件中,输出格式与输入格式相同,第一行是表头。
6.在代码中使用泛型,读文本文件使用BufferedReader,写文本文件使用BufferedWriter

解:
1.创建StudentInfo类,实现Comparable接口

  1. package com.test;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. public class StudentInfo implements Comparable<StudentInfo>
  6. {
  7. private String id;          //学号
  8. private String name;        //学生姓名
  9. private String birthday;    //出生日期
  10. private String score;       //分数
  11. public String getId()
  12. {
  13. return id;
  14. }
  15. public void setId(String id)
  16. {
  17. this.id = id;
  18. }
  19. public String getName()
  20. {
  21. return name;
  22. }
  23. public void setName(String name)
  24. {
  25. this.name = name;
  26. }
  27. public String getBirthday()
  28. {
  29. return birthday;
  30. }
  31. public void setBirthday(String birthday)
  32. {
  33. this.birthday = birthday;
  34. }
  35. public String getScore()
  36. {
  37. return score;
  38. }
  39. public void setScore(String score)
  40. {
  41. this.score = score;
  42. }
  43. /**
  44. *
  45. * {排序方法}
  46. *
  47. * @param objStu
  48. * @return
  49. * @author:LJ
  50. */
  51. public int compareTo(StudentInfo objStu)
  52. {
  53. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  54. //得分相同时按照出生日期升序排序
  55. if (this.score.equals(objStu.getScore()))
  56. {
  57. //格式化日期字符串
  58. Date date1 = null;
  59. try
  60. {
  61. date1 = dateFormat.parse(this.birthday);
  62. }
  63. catch (ParseException e1)
  64. {
  65. e1.printStackTrace();
  66. }
  67. Date date2 = null;
  68. try
  69. {
  70. date2 = dateFormat.parse(objStu.getBirthday());
  71. }
  72. catch (ParseException e2)
  73. {
  74. e2.printStackTrace();
  75. }
  76. //出生日期升序排序
  77. return date1.compareTo(date2);
  78. }
  79. //得分从高到低排序
  80. return objStu.getScore().compareTo(this.score);
  81. }
  82. }

2.读写文件类StudentFile.java

  1. package com.test;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. public class StudentFile
  11. {
  12. /**
  13. *
  14. * {根据路径读取学生文件信息}
  15. *
  16. * @param path
  17. * @return List<StudentInfo>
  18. * @throws Exception
  19. * @author:LJ
  20. */
  21. public List<StudentInfo> readFile(String path) throws Exception
  22. {
  23. List<StudentInfo> studentList = new ArrayList<StudentInfo>();
  24. BufferedReader br = null;
  25. try
  26. {
  27. br = new BufferedReader(new FileReader(path));
  28. String line = null;
  29. StudentInfo student = null;
  30. while ((line = br.readLine()) != null)
  31. {
  32. student = new StudentInfo();
  33. //将字符串分割成字符串数组
  34. String[] studentStr = line.split(",");
  35. student.setId(studentStr[0]);
  36. student.setName(studentStr[1]);
  37. student.setBirthday(studentStr[2]);
  38. student.setScore(studentStr[3]);
  39. studentList.add(student);
  40. }
  41. }
  42. catch (FileNotFoundException e)
  43. {
  44. throw new Exception("源文件未找到", e);
  45. }
  46. catch (IOException e)
  47. {
  48. throw new Exception("读文件异常.", e);
  49. }
  50. finally
  51. {//资源关闭
  52. if (br != null)
  53. {
  54. try
  55. {
  56. br.close();
  57. }
  58. catch (IOException e)
  59. {
  60. e.printStackTrace();
  61. }
  62. }
  63. }
  64. return studentList;
  65. }
  66. /**
  67. *
  68. * {将学生信息写入目标文件}
  69. *
  70. * @param studentList
  71. * @param dstPath
  72. * @throws Exception
  73. * @author:LJ
  74. */
  75. public void writeFile(List<StudentInfo> studentList, String dstPath) throws Exception
  76. {
  77. BufferedWriter bw = null;
  78. try
  79. {
  80. bw = new BufferedWriter(new FileWriter(dstPath));
  81. if (studentList != null && !studentList.isEmpty())
  82. {
  83. for(StudentInfo stu:studentList)
  84. {
  85. bw.write(stu.getId()+","+stu.getName()+","+stu.getBirthday()+","+stu.getScore());
  86. bw.newLine();//换行
  87. }
  88. }
  89. bw.flush();//强制输出缓冲区的内容,避免数据缓存,造成文件写入不完整的情况。
  90. }
  91. catch (IOException e)
  92. {
  93. throw new Exception("目标文件未找到", e);
  94. }
  95. finally
  96. {   //资源关闭
  97. if (bw != null)
  98. {
  99. try
  100. {
  101. bw.close();
  102. }
  103. catch (IOException e)
  104. {
  105. e.printStackTrace();
  106. }
  107. }
  108. }
  109. }
  110. }

3.编写main方法

    1. package com.test;
    2. import java.io.File;
    3. import java.util.ArrayList;
    4. import java.util.Collections;
    5. import java.util.List;
    6. public class Test
    7. {
    8. /**
    9. * {main方法}
    10. *
    11. * @param args
    12. * @author:LJ
    13. * @throws Exception
    14. */
    15. public static void main(String[] args) throws Exception
    16. {
    17. String srcPath = "C:" + File.separator + "test" + File.separator + "student.txt";
    18. String dstPath = "C:" + File.separator + "test" + File.separator + "result.txt";
    19. //从源文件读取学生信息
    20. StudentFile fileStu = new StudentFile();
    21. List<StudentInfo> studentList = fileStu.readFile(srcPath);
    22. //临时数组,作排序用
    23. List<StudentInfo> tempList = new ArrayList<StudentInfo>();
    24. for (int i = studentList.size()-1; i > 0; i--)
    25. {
    26. //将学生信息存入临时数组,并从原数组中删除,行标题除外
    27. tempList.add(studentList.get(i));
    28. studentList.remove(i);
    29. }
    30. //对临时数组进行排序
    31. Collections.sort(tempList);
    32. for (int i=0,n = tempList.size(); i < n; i++)
    33. {
    34. //将排序后数组追加到原数组中
    35. studentList.add(tempList.get(i));
    36. }
    37. //将学生信息写入目标文件
    38. fileStu.writeFile(studentList, dstPath);
    39. }
    40. }

BufferedReader和BufferedWriter读写文件(转载)的更多相关文章

  1. BufferedReader、BufferedWriter读写文件乱码问题:

    代码: text4500.txt文档用text打开(不知道格式): 读取会出现乱码,然后用Notepad++打开换成UTF-8格式的.就可以了

  2. BufferedReader与BufferedWriter读写中文乱码问题

    正常读写英文时用""""没问题 FileReader fre = new FileReader("E:\\TEST\\readText.txt&quo ...

  3. [转载]FileStream读写文件

    FileStream读写文件 FileStream类:操作字节的,可以操作任何的文件 StreamReader类和StreamWriter类:操作字符的,只能操作文本文件. 1.FileStream类 ...

  4. BufferedReader和BufferedWriter简介

    BufferedReader和BufferedWriter简介 为了提高字符流读写的效率,引入了缓冲机制,进行字符批量的读写,提高了单个字符读写的效率.BufferedReader用于加快读取字符的速 ...

  5. java使用IO读写文件总结

    每次用到IO的读写文件都老忘记写法,都要翻过往笔记,今天总结下,省的以后老忘.java读写文件的IO流分两大类,字节流和字符流,基类分别是字符:Reader和Writer:字节:InputStream ...

  6. JAVA基础学习之流的简述及演示案例、用缓冲区方法buffer读写文件、File类对象的使用、Serializable标记接口(6)

    1.流的简述及演示案例输入流和输出流相对于内存设备而言.将外设中的数据读取到内存中:输入将内存的数写入到外设中:输出.字符流的由来:其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表. ...

  7. Java读写文件方法总结

    Java读写文件方法总结 Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便.奈何我的记性实在是叫人着急,很多时候既 ...

  8. Java读写文件的几种方式

    自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...

  9. java中OutputStream字节流与字符流InputStreamReader 每一种基本IO流BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStr

    BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWri ...

随机推荐

  1. Compare_Connect_Letter

    题目描述: 比较两个数字mn和nm(如果mn<nm则m<n, 如果nm<mn则n<m,否则n=m) 连接这两个数字 如(mnnm) //比较两个数字mn和nm(如果mn< ...

  2. Xamarin.Android 入门之:Listview和adapter

    一.引言 不管开发什么软件,列表的使用是必不可少的,而本章我们将学习如何使用Xamarin去实现它,以及如何使用自定义适配器.关于xamarin中listview的基础和适配器可以查看官网https: ...

  3. button 事件属性

  4. 在PowerDesigner中设计概念模型

    原文:在PowerDesigner中设计概念模型 在概念模型中主要有以下几个操作和设置的对象:实体(Entity).实体属性 (Attribute).实体标识(Identifiers).关系(Rela ...

  5. Jdk命令之jps

    jps -- Java Virtual Machine Process Status Tool jps命令类似于Linux下的ps命令,可以列出本机所有正在运行的java进程.

  6. OSSEC配置文件ossec.conf中添加mysql服务

    配置路径:/opt/ossec/etc/ossec.conf <ossec_config>   <global>     <email_notification>y ...

  7. PHP 语言需要避免的 10 大误区

    PHP是一种非常流行的开源服务器端脚本语言,你在万维网看到的大多数网站都是使用php开发的.但是,你大概很奇怪的注意到有少部分的人发誓要离php 远远的.但是令人更奇怪的是或者很震惊的说他们不用php ...

  8. mac更新node

    今天在用 yeoman 的时候,提示对 npm 和 node 的版本有要求,为了决绝以后遇到的一些类似的问题,我决定定期对 node 和 npm 进行更新. npm的更新: $ sudo npm in ...

  9. Codeforces 383A - Milking cows

    原题地址:http://codeforces.com/problemset/problem/383/A 题目大意:有 n 头奶牛,全部看着左边或者右边,现在开始给奶牛挤奶,给一头奶牛挤奶时,所有能看到 ...

  10. 简单分析什么是SQL注入漏洞

    现在很多人在入侵的过程中基本都是通过SQL注入来完成的,但是有多少人知道为什么会有这样的注入漏洞呢?有的会随口说着对于字符的过滤不严造成的.但是事实是这样吗?我们学这些,不仅要知其然,更要知其所以然! ...