zoukankan      html  css  js  c++  java
  • 文件操作

    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("写入关闭失败");
                    }
                }
            }
            
        }
    }

  • 相关阅读:
    GISer面对创业的困惑
    近期微博吐槽言论存档,涉及“性能优化”、C++陋习等
    HDU 2825 Wireless Password【AC自动机+DP】
    20130809, 微软八月安全补丁提前通知
    终于把3DMAX的MSE搞定了!
    UVA 11464 Even Parity (独特思路)
    [置顶] hdu 4418 高斯消元解方程求期望
    UVA 10652 Board Wrapping
    少儿编程-教育:少儿编程教育
    少儿编程:目录
  • 原文地址:https://www.cnblogs.com/aa2178d2/p/9260529.html
Copyright © 2011-2022 走看看