Java --> sort all characters in the txt file by the code table value. java -- txt
--> How can I remove the length of excess space automatically added to the List ?... (Resolved, it is an empty character in the char array)
Package com. dragon. java. filesort; import java. io. fileReader; import java. io. fileWriter; import java. io. IOException; import java. util. arrayList; import java. util. collections; import java. util. comparator;/** sort all the characters in day19-0000.txt according to the code table value and save them to another file */public class Test {public static void main (String args []) {FileReader fr = null; fileWriter fw = null; ArrayList <Character> list = new ArrayList <> (); try {fr = new FileReader ("C: /Users/xy/Desktop/file/Java/DailyCode/08-14/src/com/dragon/java/days.txt "); fw = new FileWriter (" C: /Users/xy/Desktop/file/Java/DailyCode/08-14/src/com/dragon/java/days01.txt "); char [] buffer = new char [1024]; while (true) {int temp = fr. read (buffer, 0, buffer. length); if (temp =-1) {break;} else {
// You cannot directly traverse the buffer array, because when the array is too long, there will be free characters in it !!
// Or remove the null characters in the buffer during traversal --> if (int) buffer [I] = 0) --> the null character is 0 in the code table.
For (int I = 0; I <temp; I ++ ){
If (buffer [I] = '\ R' | buffer [I] =' \ N'
| Buffer [I] = ''| buffer [I] = '\ t '){
Continue;
}
List. add (buffer [I]);
}
} Collections. sort (list); char [] buffer1 = new char [list. size ()]; for (int I = 0; I <list. size (); I ++) {buffer1 [I] = list. get (I);} fw. write (buffer1, 0, buffer1.length);} catch (IOException e) {System. out. println (e);} finally {try {fr. close (); fw. close ();} catch (IOException e) {System. out. println (e) ;}}// compare the Character size (which can be modified independently) of the public static class MyCompartor implements Comparator <Character >{@ Override public int compare (Character o1, character o2) {// if (o1 = '\ n' | o2 =' \ n') {// return 0; //} return o1-o2 ;}}}
--> I still haven't solved the problem of extra space... (OK)