Java>BufferedReader、ArrayList、HashMap、Iterator、Properties、FileReader案例:为乱序的出师表排序
案例:为打乱顺序的出师表排序

Use Class:
BufferedReader、ArrayList、HashMap、Iterator、Properties、FileReader
以下为用到的文件,在网盘里面
链接:https://pan.baidu.com/s/1dUbNabdmH7RFstzUIPsnTA
提取码:6666
复制这段内容后打开百度网盘手机App,操作更方便哦–来自百度网盘超级会员V5的分享
public class chushibiao {public static void main(String[] args) throws IOException {// method_sort();method_load();}// path -- E:\folder_io_dome\input_chushibiao.txtpublic static void method_sort() throws IOException {// create BufferedReader object// 创建字符缓冲流BufferedReader bufferedReader = new BufferedReader(new FileReader("E:\\folder_io_dome\\input_chushibiao.txt"));String read_line_string = null;// create ArrayList // 创建ArrayList集合ArrayList<String> read_line_array = new ArrayList<>();while (true) {// get every line data// 获取每行的数据read_line_string = bufferedReader.readLine();// save data// 当本行数据不为空时,存入集合if (read_line_string != null) read_line_array.add(read_line_string);// end 当获取到的数据是null时,结束循环if (read_line_string == null) break;// output resultSystem.out.println(read_line_string);}// output array_list the length 输出集合的长度System.out.println("Read_line_arr length is : " + read_line_array.size());// 创建集合HashMap<Integer, String> hashMap = new HashMap<>();// save datafor (int i = 0; i < read_line_array.size(); i++) {// will 'char number' to 'Integer number'// 将字符数字转换为数字Integer index = Integer.parseInt(read_line_array.get(i).charAt(0) + "");// index为key string为value存入map集合中hashMap.put(index, read_line_array.get(i).replace(read_line_array.get(i).charAt(0) + ".", ""));}// ergodic hashMap 遍历集合,并将数据存入propertiesProperties properties = new Properties();// 迭代器遍历Iterator<Integer> iterator = hashMap.keySet().iterator();while (iterator.hasNext()) {Integer next = iterator.next();// print index and valueSystem.out.println(next + "-> " + hashMap.get(next));// save date on the propertiesproperties.setProperty(next + "", hashMap.get(next));}// create charOutputStream 创建字符输出流FileWriter fileWriter = new FileWriter("E:\\folder_io_dome\\sort_chushibiao.txt");// save disk 以固定格式保存到硬盘properties.store(fileWriter, "sort after");// 关闭流fileWriter.close();}// load data and show data 加载数据,并显示数据public static void method_load() throws IOException {// create charInputStream 创建字符输入流FileReader fileReader = new FileReader("E:\\folder_io_dome\\sort_chushibiao.txt");Properties properties = new Properties();// load data on the disk 自硬盘加载固定格式数据properties.load(fileReader);Iterator<String> iterator = properties.stringPropertyNames().iterator();// ergodic data on the 'properties arr'while (iterator.hasNext()){String next = iterator.next();System.out.println("Key is : " + next + " -> " + "Value is : " + properties.get(next));}// close streamfileReader.close();}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
