import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class Test02 {
public static void main(String[] args) throws Exception {
pxData();
}
//数据排序
public static void pxData() throws Exception {
ArrayList<String> list = new ArrayList<>();
//文件数组
File[] files = new File[10];
for (int i = 0; i < 10; i++) {
files[i] = new File("C:\\Users\\KONG\\Desktop\\data"+ (i+1) +".txt");
}
for (int i = 0; i < files.length ; i++) {
//建立输入流读取文件
FileReader fr = new FileReader(files[i]);
char[] c = new char[12];
while (fr.read(c) != -1){
list.add(new String(c).substring(0, 11));
}
fr.close();
//集合转数组
String[] arr = list.toArray(new String[0]);
System.out.println("排序前 :" + Arrays.toString(arr));
// 排序
arr= px1(arr);
System.out.println("排序后 :" + Arrays.toString(arr));
//数据写回文档
FileWriter fw = new FileWriter(files[i]);
for (int j = 0; j < arr.length; i++) {
fw.write(arr[j] + "\n");
fw.flush();
}
fw.close();
}
System.out.println("排序完成");
}
//数据写回
private static void outData(File file, String[] arr) throws IOException {
}
//桶排序
public static String[] px1(String[] arr){
ArrayList<String> list0 = null;
ArrayList<String> list1 = null;
ArrayList<String> list2 = null;
ArrayList<String> list3 = null;
ArrayList<String> list4 = null;
ArrayList<String> list5 = null;
ArrayList<String> list6 = null;
ArrayList<String> list7 = null;
ArrayList<String> list8 = null;
ArrayList<String> list9 = null;
for (int i = 10; i >= 0; i--) {
list0 = new ArrayList<>();
list1 = new ArrayList<>();
list2 = new ArrayList<>();
list3 = new ArrayList<>();
list4 = new ArrayList<>();
list5 = new ArrayList<>();
list6 = new ArrayList<>();
list7 = new ArrayList<>();
list8 = new ArrayList<>();
list9 = new ArrayList<>();
for (int j = 0; j < arr.length; j++) {
switch (arr[j].substring(i, i+1)) {
case ("0"):
list0.add(arr[j]);
break;
case ("1"):
list1.add(arr[j]);
break;
case ("2"):
list2.add(arr[j]);
break;
case ("3"):
list3.add(arr[j]);
break;
case ("4"):
list4.add(arr[j]);
break;
case ("5"):
list5.add(arr[j]);
break;
case ("6"):
list6.add(arr[j]);
break;
case ("7"):
list7.add(arr[j]);
break;
case ("8"):
list8.add(arr[j]);
break;
case ("9"):
list9.add(arr[j]);
break;
}
}
int k = 0,j= 0;
while (j < list0.size()){
arr[k++] = list0.get(j++);
}
j = 0;
while (j < list1.size()){
arr[k++] = list1.get(j++);
}
j = 0;
while (j < list2.size()){
arr[k++] = list2.get(j++);
}
j = 0;
while (j < list3.size()){
arr[k++] = list3.get(j++);
}
j = 0;
while (j < list4.size()){
arr[k++] = list4.get(j++);
}
j = 0;
while (j < list5.size()){
arr[k++] = list5.get(j++);
}
j = 0;
while (j < list6.size()){
arr[k++] = list6.get(j++);
}
j = 0;
while (j < list7.size()){
arr[k++] = list7.get(j++);
}
j = 0;
while (j < list8.size()){
arr[k++] = list8.get(j++);
}
j = 0;
while (j < list9.size()){
arr[k++] = list9.get(j++);
}
}
return arr;
}
/**
*快速排序算法(不稳定排序)不可用于基数排序
*/
public static String[] px(String[] arr, int begin, int end, int num){
if(begin < end) {
Integer pivot = Integer.valueOf(arr[end].substring(num, num + 1));
int i = begin, j = begin;
while (j < end) {
int now = Integer.valueOf(arr[j].substring(num, num + 1));
if (now < pivot) {
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
j++;
}
if (j == end) {
String temp = arr[end];
arr[end] = arr[i];
arr[i] = temp;
}
px(arr, begin, i - 1,num);
px(arr, i + 1, end,num);
}
return arr;
}
//创建数据
public static void cData() throws Exception {
File file = new File("C:\\Users\\KONG\\Desktop\\data01.txt");
if (!file.exists()) file.createNewFile();
PrintWriter out
= new PrintWriter(new BufferedWriter(new FileWriter(file,true)));
for (int i = 0; i < 10000; i++) {
String dataOut = "1" + String.format("%02d",new Random().nextInt(99))
+ String.format("%04d",new Random().nextInt(9999))
+ String.format("%04d",new Random().nextInt(9999)) + "\n";
out.write(dataOut);
out.flush();
}
out.close();
}
//数据拆分
public static void cfData() throws Exception {
File file = new File("C:\\Users\\KONG\\Desktop\\data01.txt");
FileReader fr = new FileReader(file);
String fileName = "C:\\Users\\KONG\\Desktop\\data1.txt";
PrintWriter pw = new PrintWriter(new FileWriter(new File(fileName),false));
int num = 1;
int time = 0;
char[] c = new char[12];
while (fr.read(c) != -1){
pw.write(c);
pw.flush();
time++;
if(time%1000 == 0){
num ++;
pw = new PrintWriter(new FileWriter(
new File(fileName.substring(0,fileName.length()-5) +num+ ".txt"),false));
}
}
pw.close();
System.out.println("拆分完成!");
}
}