A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 曹春洋 中级黑马   /  2014-1-17 16:49  /  934 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


import java.io.BufferedReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;





public class Test8 {

        /**
         * @param args
         */
       
        public static void Print() throws FileNotFoundException,IOException{
                BufferedReader br = new BufferedReader(new FileReader("E://项目/exam/src/com/itheima/a.txt"));
               
                Writer fw = null;
               
                List<Character> arrayList = new ArrayList<Character>();
                //把文件中的字符存进ArrayList集合中
                int ch;
                while((ch=br.read())!=-1){
                        char c = (char)ch;
                        arrayList.add(c);
                       
                }
                //把ArrayList集合中的值转换成数组
                Object[] sd = arrayList.toArray();
                //把数组中的值以升序进行排序
                Arrays.sort(sd);
               
                String[] sb = new String[sd.length];
                //把数组中的值放入一个字符串中
                String s =new String();
               
                for(int i=0;i<sd.length;i++){
                        sb[i]=sd[i].toString();
                        s=s.concat(sb[i].trim());
                       
                }
                try{
                        //把字符串的值写入b.txt文件中
                        fw = new FileWriter("E://项目/exam/src/com/itheima/b.txt");
                        fw.write(s);
                }catch(IOException e){
                        e.printStackTrace();
                }finally{
                        try{
                                if(null != fw){
                                        fw.close();
                                }
                        }catch(IOException e){
                                        e.printStackTrace();
                        }
                       
                }
               
               
        }
       
       
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                try {
                        Print();
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

        }

}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马