![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c83e5496e17a47d4fa3aa9e7e0811b3e.png)
注意:
1.Java 按行读取UTF-8文件,开头第一行总会有一个不可见字符的问题
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5cca89dc3ae4756f0590eae0bb6c0c16.png)
教程程序:
package Practice;
import java.io.*;
import java.util.HashMap;
import java.util.Set;
public class Demo02 {
public static void main(String[] args) throws IOException {
HashMap<String,String> map = new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader("G:\\Java\\测试文件夹\\出师表乱序.txt"));
BufferedWriter bw =new BufferedWriter(new FileWriter("G:\\Java\\测试文件夹\\出师表正序.txt"));
String line;
while ((line=br.readLine())!=null){
String[] arr =line.split("\\.");
map.put(arr[0],arr[1]);
}
Set<String> set = map.keySet();
for (String i:set){
bw.write(i+"."+map.get(i));
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/688c9735d1afb467780a7bd6573744ef.png)
改进程序:
package Practice;
import java.io.*;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;
public class Demo02 {
public static void main(String[] args) throws IOException {
HashMap<String,String> map = new HashMap<>();
Properties pro = new Properties();
BufferedReader br = new BufferedReader(new FileReader("G:\\Java\\测试文件夹\\出师表乱序.txt"));
BufferedWriter bw =new BufferedWriter(new FileWriter("G:\\Java\\测试文件夹\\出师表正序.txt"));
String line;
while ((line=br.readLine())!=null){
String[] arr =line.split("\\.");
pro.setProperty(arr[0],arr[1]);
}
pro.store(bw,"出师表正序");
bw.close();
br.close();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0403e978a37c196fa7d7ebf2486edeec.png)
问题:HashMap<k,v>
为无序集合,其内部排序是根据key的Hash值,所以使用HashMap<k,v>
只试用于前面序号为0-9,才可以自动排序,如果再大就会乱序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9132557b2ae66c3b9805dce19f342fb3.png)
教程程序结果(错误):
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7cdc9a3d1f0c066e4c576d066fcfd889.png)
解决方案:可以使用TreeMap<k,v>
或者Collections.sort
,对key进行排序
package Practice;
import java.io.*;
import java.util.*;
public class Demo02 {
public static void main(String[] args) throws IOException {
HashMap<String, String> map = new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader("G:\\Java\\测试文件夹\\乱序多段落测试.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("G:\\Java\\测试文件夹\\多段落测试02.txt"));
String line;
while ((line = br.readLine()) != null) {
String[] arr = line.split("\\.");
map.put(arr[0], arr[1]);
}
Set<String> set = map.keySet();
List<String> list = new ArrayList<>(set);
ArrayList<Integer> arrayInt = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
arrayInt.add(Integer.parseInt(list.get(i)));
}
Collections.sort(arrayInt);
ArrayList<String> arrayStr = new ArrayList<>();
for (int i = 0; i <arrayInt.size() ; i++) {
arrayStr.add(Integer.toString(arrayInt.get(i)));
}
for (String i : arrayStr) {
bw.write(i + "." + map.get(i));
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/802325c337690a84c3623e3ced7892ef.png)