首页 > 编程知识 正文

java 读取文件顺序,java程序 从文件读取指定内容写出

时间:2023-05-03 13:28:40 阅读:219213 作者:2062

Java读取文件内容并排序

写这篇博客的初衷是由于参加了一次笔试但未完成。网上查资料但很多代码我发现多多少少都有些问题,所以自己重新写了一下代码。一方面是为了自己,希望下次出现类似的问题能够迅速解决,另一方面也希望能够帮一帮一些有该需求的朋友吧。

根据内容排序我用了2种方法:数组和集合。以下为代码实现:

/** * @author zong * */public class Practice_day09_01_Stream {public static void main(String[] args) {//二维数组System.out.println("二维数组:");FirstFile fif = new FirstFile();fif.sortFile();System.out.println("==============================我是华丽的分割线==================================");//集合System.out.println("集合:");SecondFile sef = new SecondFile();sef.sortFile();}}/** * 需求:读取文件中的内容并根据成绩排序。 * !: 此处用的是二维数组 * 步骤: * 1.先创建流按行读取文件; * 2.每读取的一行文件内容用正则表达式将每行的元素切割并放入一个一维数组中; * 3.将一维数组放入二维数组中; * 4.对二维数组进行排序。 * 文件内容: * 6100020001 章三 91 *6100020002 李四 88 *6100020003 王五 79 *6100020004 赵三 91 *6100020005 魏四 88 *6100020006 程五 79 *6100020007 贺六 90 *6100020008 江七 93 *6100020009 姜八 84 *6100020010 吴六 90 *6100020011 胡七 93 * * @author zong * @param file * @exception UnsupportedEncodingException FileNotFoundException IOException */class FirstFile {public Object[][] ArraysFile(){String pathname = "H:/a.txt";File file = new File(pathname);BufferedReader br = null;InputStreamReader isr = null;FileInputStream fis = null;String encoding = "GBK";Object[][] strtwo = new Object[11][3];try {//创建缓冲流读取文件内容fis = new FileInputStream(file);isr = new InputStreamReader(fis, encoding);br = new BufferedReader(isr);String line = "";int i = 0;//读取每行内容直到为空while ((line = br.readLine()) != null) {//用正则表达式去空格:trim()去掉首尾多余空格,.split("\s+")表示按多个空格切割String[] strsplit = line.trim().split("\s+");//将切割完的元素放入一维数组中,再将一维数组存入二维数组中:{{xxx,"xx",xx},{xxx,"xx",xx},{xxx,"xx",xx}}for (int j = 0; j < strsplit.length; j++) {strtwo[i][j] = strsplit[j];}i++;}} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (br != null) {br.close();}if (isr != null) {isr.close();}if (fis != null) {fis.close();}} catch (IOException e) {e.printStackTrace();}}return strtwo;}public void sortFile() {Object[][] strtwo = ArraysFile();//根据成绩排序:System.out.println("排序前:");for (int j = 0; j < strtwo.length; j++) {System.out.println(Arrays.toString(strtwo[j]));}//选择排序for (int j = 0; j < strtwo.length; j++) {for (int k = j + 1; k < strtwo.length; k++) {//定义交换顺序条件int l = Integer.valueOf(strtwo[j][2].toString());int m = Integer.valueOf(strtwo[k][2].toString());//二维数组中的小一维数组的元素只是作为交换数组顺序的条件,而需要交换顺序的是含有该元素的一维数组。if (l < m) {Object[] temp = strtwo[j];strtwo[j] = strtwo[k];strtwo[k] = temp;}}}//排序后的数组:System.out.println("排序后:");for (int j = 0; j < strtwo.length; j++) {System.out.println(Arrays.toString(strtwo[j]));}}}/** * 需求:读取文件中的内容并根据成绩排序。 * !: 此处用的是ArrayList集合 * 步骤: * 1.先创建流按行读取文件; * 2.每读取的一行文件内容用正则表达式将每行的元素切割 * 3.创建Students对象获取数据并存入集合 * 4.对集合进行排序。 * * @author zong * @param file * @exception UnsupportedEncodingException FileNotFoundException IOException * */class SecondFile {public List<Students> listStudents(){String pathname = "H:/a.txt";File file = new File(pathname);BufferedReader br = null;InputStreamReader isr = null;FileInputStream fis = null;String encoding = "GBK";List<Students> stuList = new ArrayList<Students>();try {//创建缓冲流读取文件内容fis = new FileInputStream(file);isr = new InputStreamReader(fis, encoding);br = new BufferedReader(isr);String line = "";//读取文件直到内容为空while ((line = br.readLine()) != null) {//用正则表达式去空格:trim()去掉首尾多余空格,.split("\s+")表示按多个空格切割String[] strsplit = line.trim().split("\s+");long id = Long.valueOf(strsplit[0]);int grade = Integer.valueOf(strsplit[2]);Students stu = new Students();stu.setId(id);stu.setName(strsplit[1]);stu.setGrade(grade);stuList.add(stu);}} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (br != null) {br.close();}if (isr != null) {isr.close();}if (fis != null) {fis.close();}} catch (IOException e) {e.printStackTrace();}}return stuList;}public void sortFile(){List<Students> stuList = listStudents();//排序集合Collections.sort(stuList, new Comparator<Students>() {@Overridepublic int compare(Students o1, Students o2) {int comp = String.valueOf(o2.getGrade()).compareTo(String.valueOf(o1.getGrade()));return comp;}});//排序后遍历集合for (Students stus : stuList) {System.out.println("姓名:" + stus.getName() + " 学号:" + stus.getId() + " 成绩:" + stus.getGrade());}}}class Students {//学生idprivate Long id;//学生姓名private String name;//学生分数private Integer grade;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getGrade() {return grade;}public void setGrade(Integer grade) {this.grade = grade;}}

以上2种方法推荐用集合,数组的局限性太大。如果以上代码有什么问题欢迎提出指点。

如果需要转载请标注:https://blog.csdn.net/weixin_42532346/article/details/90042339

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。