import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test8 {
/**
* @param args
*/
public static void Print() throws FileNotFoundException,IOException{
BufferedReader br = new BufferedReader(new FileReader("E://项目/exam/src/com/itheima/a.txt"));
Writer fw = null;
List<Character> arrayList = new ArrayList<Character>();
//把文件中的字符存进ArrayList集合中
int ch;
while((ch=br.read())!=-1){
char c = (char)ch;
arrayList.add(c);
}
//把ArrayList集合中的值转换成数组
Object[] sd = arrayList.toArray();
//把数组中的值以升序进行排序
Arrays.sort(sd);
String[] sb = new String[sd.length];
//把数组中的值放入一个字符串中
String s =new String();
for(int i=0;i<sd.length;i++){
sb[i]=sd[i].toString();
s=s.concat(sb[i].trim());
}
try{
//把字符串的值写入b.txt文件中
fw = new FileWriter("E://项目/exam/src/com/itheima/b.txt");
fw.write(s);
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(null != fw){
fw.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Print();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|
|