• 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接口

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

    2.读写文件类StudentFile.java

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

    3.编写main方法

      1. package com.test;  
      2.  
      3. import java.io.File;  
      4. import java.util.ArrayList;  
      5. import java.util.Collections;  
      6. import java.util.List;  
      7.  
      8. public class Test  
      9. {  
      10.     /**  
      11.      * {main方法}  
      12.      *   
      13.      * @param args  
      14.      * @author:LJ  
      15.      * @throws Exception   
      16.      */ 
      17.  
      18.     public static void main(String[] args) throws Exception  
      19.     {  
      20.         String srcPath = "C:" + File.separator + "test" + File.separator + "student.txt";  
      21.         String dstPath = "C:" + File.separator + "test" + File.separator + "result.txt";  
      22.         //从源文件读取学生信息  
      23.         StudentFile fileStu = new StudentFile();  
      24.         List<StudentInfo> studentList = fileStu.readFile(srcPath);  
      25.         //临时数组,作排序用  
      26.         List<StudentInfo> tempList = new ArrayList<StudentInfo>();  
      27.         for (int i = studentList.size()-1; i > 0; i--)  
      28.         {  
      29.             //将学生信息存入临时数组,并从原数组中删除,行标题除外  
      30.             tempList.add(studentList.get(i));  
      31.             studentList.remove(i);  
      32.         }  
      33.         //对临时数组进行排序  
      34.         Collections.sort(tempList);  
      35.         for (int i=0,n = tempList.size(); i < n; i++)  
      36.         {  
      37.             //将排序后数组追加到原数组中  
      38.             studentList.add(tempList.get(i));  
      39.         }  
      40.         //将学生信息写入目标文件  
      41.         fileStu.writeFile(studentList, dstPath);  
      42.     }  
  • 相关阅读:
    3.使用Unity 创建自己的android AR 项目 (小白篇)
    2.关于Unity -Vuforia -Android 开发 ,平台的搭建(极品菜鸟完整版) 续
    (番外篇) 高通 AR Unity 虚拟按钮 -源于 官网
    浅谈HTTP协议与RESTful
    深入浅出浮点型
    华杰简易入门系列之正则表达式——基础篇
    50行实现简易HTTP服务器
    Android中SD卡内容读取和简易FTP文件上传(番外)
    Android中谷歌语音识别应用探究
    Linux系统安装Nodejs(4.4.7)
  • 原文地址:https://www.cnblogs.com/phoenixzq/p/3413435.html
Copyright © 2020-2023  润新知