package test01;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
public class TestThree {
public static void main(String[] args) {
// TODO Auto-generated method stub
String a = readFile("E:/a.txt");
String b = readFile("E:/b.txt");
StringBuffer sb = new StringBuffer();
sb.append(a).append(b);
char[] aa = sb.toString().toCharArray();
Arrays.sort(aa);
writeFile(String.valueOf(aa));
}
public static String readFile(String filepath){
File file = new File(filepath);
StringBuilder sb = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String s = null;
try {
while((s=br.readLine())!=null){
sb.append(s);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return sb.toString();
}
public static void writeFile(String data){
File file = new File("E:/20180420test.txt");
if(!(file.exists())){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fo = new FileOutputStream(file);
byte[] by = data.getBytes();
try {
fo.write(by);
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
亲测有效哦~Java 代码量还是比 Python 多好多~