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. java:打包

    包名命名规范: 1.包名全部小写 2.包名一般情况下是域名的倒过来写+个性命名,如:tinyphp.com,就写成com.tinyphp+.xxx 打包方法 package + 包名 package ...

  2. 本人arcgis api for javascript中常见错误总结

    1. 2.对象不支持"replace"属性或方法 解决办法:一般在ie中执行js会报这样的错误,基本问题就是你引用了某个对象中不存在的方法,可能是这个方法本来存在而你写错了,或者调 ...

  3. 喵星人教你记 HTTP 状态码

    记忆HTTP状态码是有一些困难的,因为状态码很多且很难记忆.GirlieMac,也就是Tomomi Imura利用她巧妙的构思,PS了一系列的HTTP状态信息.在你看过这些图片之后,你绝对可以记住一些 ...

  4. Ossec常用命令

    启动并查看httpd服务 systemctl start httpd systemctl status httpd.service 启动并查看mysql服务 systemctl start maria ...

  5. ubuntu 映射网络驱动器到本地

    公司办公都是在ubuntu服务器上,所以每每拷贝修改文件都要ftp之类的,实在不方便. 索性将服务器挂载到自己本地目录下. 服务器端参考其他samba安装和配置.这里只是说下本地自动挂载方法. 一.首 ...

  6. thrift总结

    定义: Apache Thrift是一个facebook建立的RPC框架,现在是一个Apache的顶级项目.Thrift允许通过一个跨语言的定义文件的方式定义数据类型和服务接口,[这个文件]作为[RP ...

  7. 经典K线组合图解 > 正文

    日K线波段中上下影线的箱体操作法(完整) http://video.sina.com.cn/v/b/130809461-2486130757.html!!经典K线组合图解 > 正文 http:/ ...

  8. STL源码中map和set中key值不能修改的实现

    前言 最近正好刚刚看完,<stl源码剖析>这本书的map和set的源码部分.但是看完之后又突然发现,之前怎么没有注意到map和set容器中key不能修改是怎么实现的.故,特此整理如下. s ...

  9. poj 2029 Get Many Persimmon Trees (dp)

    题目链接 又是一道完全自己想出来的dp题. 题意:一个w*h的图中,有n个点,给一个s*t的圈,求这个圈能 圈的最多的点 分析:d[i][j]代表i行j列 到第一行第一列的这个方框内有多少个点, 然后 ...

  10. 纯tarjan poj2186

    tarjan,叙叙旧咯 #include<cstdio>#define maxn 50005int e[maxn],ne[maxn],be[maxn],all;int DFN[maxn], ...