Java -- & gt; sorts all characters in the txt file by the code table value. java -- txt

Source: Internet
Author: User

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)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.