a- package test;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Arrays;
- public class ReadAndSort {
- /**
- * 需求:读取s.txt里的字符串,排序后存到ss.txt中
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- //建立读取文件流
- BufferedReader br = new BufferedReader(new FileReader("s.txt"));
- //读取字符串
- String str = br.readLine();
- System.out.println("读取的字符串:"+str);
- //对字符排序
- char[] chs = str.toCharArray();
- Arrays.sort(chs);
- //关闭读取资源
- br.close();
- //存入文档
- BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\ss.txt"));
- bw.write(String.valueOf(chs));
- //刷新并关闭流
- bw.close();
- System.out.println("copy and sort done!");
-
- }
- }
复制代码
|
|