Sorting a text file in Java

前端 未结 3 1702
星月不相逢
星月不相逢 2021-01-07 00:25

I have a text file with a list of words which I need to sort in alphabetical order using Java. The words are located on seperate lines.

How would I go about this, R

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-07 00:49

    import java.io.*;
    import java.util.*;
    
    public class example
    {
        TreeSet tree=new TreeSet();
        public static void main(String args[])
        {
            new example().go();
        }
        public void go()
    
        {
            getlist();
            System.out.println(tree);
    
        }
         void getlist()
        {
            try
            {
                File myfile= new File("C:/Users/Rajat/Desktop/me.txt");
                BufferedReader reader=new BufferedReader(new FileReader(myfile));
                String line=null;
                while((line=reader.readLine())!=null){
                    addnames(line);
    
    
                }
            reader.close();
            }
    
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
    
        }
        void addnames(String a)
        {
               tree.add(a);
               for(int i=1;i<=a.length();i++)
               {
    
               }
        }
    }
    

提交回复
热议问题