BufferedReader和BufferedWriter读写文件(转载)
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接口
- package com.test;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class StudentInfo implements Comparable<StudentInfo>
- {
- private String id; //学号
- private String name; //学生姓名
- private String birthday; //出生日期
- private String score; //分数
- public String getId()
- {
- return id;
- }
- public void setId(String id)
- {
- this.id = id;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public String getBirthday()
- {
- return birthday;
- }
- public void setBirthday(String birthday)
- {
- this.birthday = birthday;
- }
- public String getScore()
- {
- return score;
- }
- public void setScore(String score)
- {
- this.score = score;
- }
- /**
- *
- * {排序方法}
- *
- * @param objStu
- * @return
- * @author:LJ
- */
- public int compareTo(StudentInfo objStu)
- {
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- //得分相同时按照出生日期升序排序
- if (this.score.equals(objStu.getScore()))
- {
- //格式化日期字符串
- Date date1 = null;
- try
- {
- date1 = dateFormat.parse(this.birthday);
- }
- catch (ParseException e1)
- {
- e1.printStackTrace();
- }
- Date date2 = null;
- try
- {
- date2 = dateFormat.parse(objStu.getBirthday());
- }
- catch (ParseException e2)
- {
- e2.printStackTrace();
- }
- //出生日期升序排序
- return date1.compareTo(date2);
- }
- //得分从高到低排序
- return objStu.getScore().compareTo(this.score);
- }
- }
2.读写文件类StudentFile.java
- package com.test;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class StudentFile
- {
- /**
- *
- * {根据路径读取学生文件信息}
- *
- * @param path
- * @return List<StudentInfo>
- * @throws Exception
- * @author:LJ
- */
- public List<StudentInfo> readFile(String path) throws Exception
- {
- List<StudentInfo> studentList = new ArrayList<StudentInfo>();
- BufferedReader br = null;
- try
- {
- br = new BufferedReader(new FileReader(path));
- String line = null;
- StudentInfo student = null;
- while ((line = br.readLine()) != null)
- {
- student = new StudentInfo();
- //将字符串分割成字符串数组
- String[] studentStr = line.split(",");
- student.setId(studentStr[0]);
- student.setName(studentStr[1]);
- student.setBirthday(studentStr[2]);
- student.setScore(studentStr[3]);
- studentList.add(student);
- }
- }
- catch (FileNotFoundException e)
- {
- throw new Exception("源文件未找到", e);
- }
- catch (IOException e)
- {
- throw new Exception("读文件异常.", e);
- }
- finally
- {//资源关闭
- if (br != null)
- {
- try
- {
- br.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- return studentList;
- }
- /**
- *
- * {将学生信息写入目标文件}
- *
- * @param studentList
- * @param dstPath
- * @throws Exception
- * @author:LJ
- */
- public void writeFile(List<StudentInfo> studentList, String dstPath) throws Exception
- {
- BufferedWriter bw = null;
- try
- {
- bw = new BufferedWriter(new FileWriter(dstPath));
- if (studentList != null && !studentList.isEmpty())
- {
- for(StudentInfo stu:studentList)
- {
- bw.write(stu.getId()+","+stu.getName()+","+stu.getBirthday()+","+stu.getScore());
- bw.newLine();//换行
- }
- }
- bw.flush();//强制输出缓冲区的内容,避免数据缓存,造成文件写入不完整的情况。
- }
- catch (IOException e)
- {
- throw new Exception("目标文件未找到", e);
- }
- finally
- { //资源关闭
- if (bw != null)
- {
- try
- {
- bw.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- }
3.编写main方法
- package com.test;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- public class Test
- {
- /**
- * {main方法}
- *
- * @param args
- * @author:LJ
- * @throws Exception
- */
- public static void main(String[] args) throws Exception
- {
- String srcPath = "C:" + File.separator + "test" + File.separator + "student.txt";
- String dstPath = "C:" + File.separator + "test" + File.separator + "result.txt";
- //从源文件读取学生信息
- StudentFile fileStu = new StudentFile();
- List<StudentInfo> studentList = fileStu.readFile(srcPath);
- //临时数组,作排序用
- List<StudentInfo> tempList = new ArrayList<StudentInfo>();
- for (int i = studentList.size()-1; i > 0; i--)
- {
- //将学生信息存入临时数组,并从原数组中删除,行标题除外
- tempList.add(studentList.get(i));
- studentList.remove(i);
- }
- //对临时数组进行排序
- Collections.sort(tempList);
- for (int i=0,n = tempList.size(); i < n; i++)
- {
- //将排序后数组追加到原数组中
- studentList.add(tempList.get(i));
- }
- //将学生信息写入目标文件
- fileStu.writeFile(studentList, dstPath);
- }
- }
BufferedReader和BufferedWriter读写文件(转载)的更多相关文章
- BufferedReader、BufferedWriter读写文件乱码问题:
代码: text4500.txt文档用text打开(不知道格式): 读取会出现乱码,然后用Notepad++打开换成UTF-8格式的.就可以了
- BufferedReader与BufferedWriter读写中文乱码问题
正常读写英文时用""""没问题 FileReader fre = new FileReader("E:\\TEST\\readText.txt&quo ...
- [转载]FileStream读写文件
FileStream读写文件 FileStream类:操作字节的,可以操作任何的文件 StreamReader类和StreamWriter类:操作字符的,只能操作文本文件. 1.FileStream类 ...
- BufferedReader和BufferedWriter简介
BufferedReader和BufferedWriter简介 为了提高字符流读写的效率,引入了缓冲机制,进行字符批量的读写,提高了单个字符读写的效率.BufferedReader用于加快读取字符的速 ...
- java使用IO读写文件总结
每次用到IO的读写文件都老忘记写法,都要翻过往笔记,今天总结下,省的以后老忘.java读写文件的IO流分两大类,字节流和字符流,基类分别是字符:Reader和Writer:字节:InputStream ...
- JAVA基础学习之流的简述及演示案例、用缓冲区方法buffer读写文件、File类对象的使用、Serializable标记接口(6)
1.流的简述及演示案例输入流和输出流相对于内存设备而言.将外设中的数据读取到内存中:输入将内存的数写入到外设中:输出.字符流的由来:其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表. ...
- Java读写文件方法总结
Java读写文件方法总结 Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便.奈何我的记性实在是叫人着急,很多时候既 ...
- Java读写文件的几种方式
自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...
- java中OutputStream字节流与字符流InputStreamReader 每一种基本IO流BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStr
BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWri ...
随机推荐
- python logging 学习笔记
logging.basicConfig函数各参数: filename: 指定日志文件名 filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a' format: 指定输出的格 ...
- MFC、WTL、WPF、wxWidgets、Qt、GTK、Cocoa、VCL 各有什么特点?
WTL都算不上什么Framework,就是利用泛型特性对Win API做了层封装,设计思路也没摆脱MFC的影响,实际上用泛型做UI Framework也只能算是一次行为艺术,这个思路下继续发展就会变得 ...
- Arcgis Engine最短路径分析
ArcEngine 最短路径分析(源码) using System; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geometry; using ESRI ...
- Spring与Struts2整合VS Spring与Spring MVC整合
Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的 ...
- Linux 动画显示
Linux最强大的一个特征就是它有大量的各种小命令工具,这也可以称做是它最有趣的一个地方了.在这些大量的有用的命令和脚本中,你会发现有少部 分命令工具不那么有用的——如果你不愿意说是完全没用处的话.你 ...
- 未能加载文件或程序集“Interop.jmail”或它的某一个依赖项
未能加载文件或程序集“Interop.jmail”或它的某一个依赖项.试图加载格式不正确的程序. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中 ...
- 浏览器 怪异模式(Quirks Mode) 与 标准模式(Standards Mode)
浏览器 怪异模式(Quirks Mode) 与 标准模式(Standards Mode) 怪异模式,浏览器使用自己的方式解析渲染页面,在不同的浏览器就会显示不同的样式.标准模式,浏览器使用W3C的标准 ...
- 三星原厂就K9K8G08U0D升级为K9K8G08U0E的回信
1. please check the below timing first. K9F1G08U0E vs K9F1G08U0D Timing difference tR tPROG tBERS N ...
- struct TABLE_SHARE
struct TABLE_SHARE { TABLE_SHARE() {} /* Remove gcc warning */ /** Category of this table. */ TABLE_ ...
- span元素定义宽高度
由于span是行内元素,不可能有高度和宽度的,在span标签里添加内容,可以撑出来宽高,想要定义宽高必须转话成块级元素. span{ display:block; } 或者使用 span{ displ ...