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

    package 文件读取;
    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("写入关闭失败");
                    }
                }
            }
            
        }
    }

  • 相关阅读:
    python-实现选择排序
    python-实现冒泡排序
    python-实现双端队列
    python-实现队列结构
    python-实现单向循环链表
    类型转换
    uint64_t类型输出为十六进制格式
    python文件的写入与读出
    linux系统中安装虚拟机
    IPv4和IPv6地址的存取
  • 原文地址:https://www.cnblogs.com/kongdexiu-13/p/9260408.html
Copyright © 2011-2022 走看看