本帖最后由 micro_hx 于 2015-6-13 17:08 编辑
- package com.test1;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Arrays;
- public class Test3 {
- public static void main(String[] args) {
- BufferedReader reader = null ;
- BufferedWriter writer = null ;
-
- try {
- reader = new BufferedReader(new FileReader(new File("D:/1.txt")));
- String result = null ;
- StringBuffer sb = new StringBuffer();
- while((result = reader.readLine()) != null){
- sb.append(result);
- }
- char[] target = sb.toString().toCharArray() ;
- Arrays.sort(target);
- writer = new BufferedWriter(new FileWriter(new File("D:/2.txt")));
- writer.write(target);
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- if(reader!=null){
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(writer!=null){
- try {
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
复制代码 |