zoukankan      html  css  js  c++  java
  • IO流的练习5 —— 读取文件中的字符串,排序后写入另一文件中

    需求:已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”
        请编写程序读取数据内容,把数据排序后写入ss.txt中。
    分析:
      A:读取文件中的数据
      B:把数据存在一个字符串中
      C:把字符串转换成字符串数组
      D:对字符串数组进行排序
      E:数组转换成字符串
      F:把字符串写入文件中

     1 public static void main(String[] args) throws IOException {
     2         // 读取文件中的数据 字符缓冲输入流
     3         BufferedReader br = new BufferedReader(new FileReader("s.txt"));
     4         // 把数据存在一个字符串中
     5         String s = br.readLine();
     6         br.close();
     7         // 把字符串转换成字符串数组
     8         char[] ch = s.toCharArray();
     9         // 对字符串数组进行排序
    10         Arrays.sort(ch);
    11         //数组转换成字符串
    12         String result = new String(ch);
    13         
    14         //把字符串写入文件中 字符缓冲输出流
    15         BufferedWriter bw = new BufferedWriter(new FileWriter("ss.txt"));
    16         bw.write(result);
    17         bw.close();
    18 
    19     }
    何事都只需坚持.. 难? 维熟尔。 LZL的自学历程...只需坚持
  • 相关阅读:
    CSS display使用
    WPF触发器
    WPF动画2
    WPF动画2
    WPF 动画1
    CSS 媒体查询
    [Leetcode] Rotate List
    [Leetcode] Add Two Numbers
    [Leetcode] Sort List
    [Leetcode] Reverse Linked List II
  • 原文地址:https://www.cnblogs.com/LZL-student/p/5927798.html
Copyright © 2011-2022 走看看