文件操作

import java.io.*;
import java.util.*;
class  File
{
    public static void main(String[] args)
    {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try
        {
            //创建读取流
            br = new BufferedReader(new FileReader("C:\\a.txt"));
            //创建写入流
            bw = new BufferedWriter(new FileWriter("C:\\b.txt"));
            //读取a.txt中的字符串内容
            String s = br.readLine();
            //将字符串转变成字符串数组
            char[] a = s.toCharArray();
            //对数组中的元素按按自然顺序排序
            Arrays.sort(a);
            //将数组中写入到输出流
            bw.write(a, 0, a.length);
            bw.flush();
        }
        //异常处理
        catch(IOException e)
        {
            throw new RuntimeException("读写失败");
        }
        finally
        {
            //关闭读取流
            if(br!=null)
            {
                try
                {
                    br.close();
                }
                catch(IOException e)
                {
                    throw new RuntimeException("读取关闭失败");
                }
            }
            //关闭写入流
            if(bw!=null)
            {
                try
                {
                    bw.close();
                }
                catch(IOException e)
                {
                    throw new RuntimeException("写入关闭失败");
                }
            }
        }
        
    }
}

posted @ 2018-07-03 20:44  G小斌  阅读(83)  评论(0编辑  收藏  举报