import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class FileUtils {
public static void reaSortAndOutPutFile(String filepath, String destFilePath)
throws FileNotFoundException, IOException {
File file = new File(filepath);
if (!file.isDirectory()) {
int[] arr = readFileByLines(file);
writeFile(arr, destFilePath, file.getName());
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + File.separator + filelist[i]);
if (!readfile.isDirectory()) {
int[] arr = readFileByLines(readfile);
writeFile(arr, destFilePath, readfile.getName());
} else if (readfile.isDirectory()) {
reaSortAndOutPutFile(filepath + File.separator + filelist[i], destFilePath);
}
}
}
}
public static void writeFile(int[] arr, String destFilePath, String fileName) {
File file = new File(destFilePath);
if (!file.exists()) {
file.mkdir();
}
int maxTimes = RadixSort.getMaxTime(arr);
RadixSort.radixSort(arr, maxTimes);
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(destFilePath.concat(fileName));
for (int i = 0; i < arr.length; i++) {
fileWriter.write(arr[i] + System.getProperty("line.separator"));
}
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static int[] readFileByLines(File file) throws IOException {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
int num = Integer.parseInt(reader.readLine());
int[] arr = new int[num];
String tempString = null;
int j = 0;
while ((tempString = reader.readLine()) != null) {
arr[j] = Integer.parseInt(tempString);
j++;
}
reader.close();
return arr;
} catch (IOException e) {
throw e;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
public static void unpack(String zip, String outputDir, String charsetName) {
unpack(new File(zip), new File(outputDir), charsetName);
}
public static void unpack(File zip, File outputDir, String charsetName) {
FileOutputStream out = null;
InputStream in = null;
ZipFile zipFileData = null;
ZipFile zipFile = null;
try {
if (outputDir != null && !outputDir.exists()) {
outputDir.mkdirs();
}
if (charsetName != null && charsetName != "") {
zipFile = new ZipFile(zip.getPath(), Charset.forName(charsetName));
} else {
zipFile = new ZipFile(zip.getPath(), Charset.forName("utf8"));
}
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String filePath = "";
if (outputDir == null) {
filePath = zip.getParentFile().getPath() + File.separator + entry.getName();
} else {
filePath = outputDir.getPath() + File.separator + entry.getName();
}
File file = new File(filePath);
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
if (parentFile.isDirectory()) {
continue;
}
}
if (charsetName != null && charsetName != "") {
zipFileData = new ZipFile(zip.getPath(), Charset.forName(charsetName));
} else {
zipFileData = new ZipFile(zip.getPath(), Charset.forName("utf8"));
}
Enumeration<? extends ZipEntry> entriesData = zipFileData.entries();
while (entriesData.hasMoreElements()) {
ZipEntry entry = entriesData.nextElement();
in = zipFile.getInputStream(entry);
String filePath = "";
if (outputDir == null) {
filePath = zip.getParentFile().getPath() + File.separator + entry.getName();
} else {
filePath = outputDir.getPath() + File.separator + entry.getName();
}
File file = new File(filePath);
if (file.isDirectory()) {
continue;
}
out = new FileOutputStream(filePath);
int len = -1;
byte[] bytes = new byte[1024];
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
zipFile.close();
zipFileData.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static List<String> getFileFullName(String sourceFilePath) throws IOException {
List<String> fileNames = new ArrayList<>();
File f = new File(sourceFilePath);
File[] files = f.listFiles();
for (File temp : files) {
fileNames.add(temp.getCanonicalPath());
}
return fileNames;
}
}
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
String zipDir = "/Users/liujiaqiang/Desktop/test.zip";
String outputDir = "/Users/liujiaqiang/Desktop/zip";
String sortDir = "/Users/liujiaqiang/Desktop/sortDir/";
FileUtils.unpack(zipDir, outputDir, "utf-8");
FileUtils.reaSortAndOutPutFile(outputDir, sortDir);
List<String> fileNames = FileUtils.getFileFullName(sortDir);
MergeSort sorter = new MergeSort(fileNames, "output.txt");
sorter.sort();
sorter.close();
}
}
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MergeSort implements Closeable {
private List<MergeSource> sources;
private MergeOut out;
public MergeSort(List<String> files, String outFilename) {
this.sources = new ArrayList<>();
for (String filename : files) {
this.sources.add(new MergeSource(filename));
}
this.out = new MergeOut(outFilename);
}
static class MergeOut implements Closeable {
private PrintWriter writer;
public MergeOut(String filename) {
try {
this.writer = new PrintWriter(new FileOutputStream(filename));
} catch (FileNotFoundException e) {
}
}
public void write(Bin bin) {
writer.println(String.format("%04d", bin.num));
}
@Override
public void close() throws IOException {
writer.flush();
writer.close();
}
}
static class MergeSource implements Closeable {
private BufferedReader reader;
private String cachedLine;
public MergeSource(String filename) {
try {
FileReader fr = new FileReader(filename);
this.reader = new BufferedReader(fr);
} catch (FileNotFoundException e) {
}
}
public boolean hasNext() {
String line;
try {
line = this.reader.readLine();
if (line == null || line.isEmpty()) {
return false;
}
this.cachedLine = line.trim();
return true;
} catch (IOException e) {
}
return false;
}
public int next() {
if (this.cachedLine == null) {
if (!hasNext()) {
throw new IllegalStateException("no content");
}
}
int num = Integer.parseInt(this.cachedLine);
this.cachedLine = null;
return num;
}
@Override
public void close() throws IOException {
this.reader.close();
}
}
static class Bin implements Comparable<Bin> {
int num;
MergeSource source;
Bin(MergeSource source, int num) {
this.source = source;
this.num = num;
}
@Override
public int compareTo(Bin o) {
return o.num - this.num;
}
}
public List<Bin> prepare() {
List<Bin> bins = new ArrayList<>();
for (MergeSource source : sources) {
Bin newBin = new Bin(source, source.next());
bins.add(newBin);
}
Collections.sort(bins);
return bins;
}
public void sort() {
List<Bin> bins = prepare();
while (true) {
MergeSource current = bins.get(0).source;
if (current.hasNext()) {
Bin newBin = new Bin(current, current.next());
int index = Collections.binarySearch(bins, newBin);
if (index == 0 || index == -1) {
this.out.write(newBin);
if (index == -1) {
throw new IllegalStateException("impossible");
}
} else {
if (index < 0) {
index = -index - 1;
}
bins.add(index, newBin);
Bin minBin = bins.remove(0);
this.out.write(minBin);
}
} else {
Bin minBin = bins.remove(0);
this.out.write(minBin);
if (bins.isEmpty()) {
break;
}
}
}
}
@Override
public void close() throws IOException {
for (MergeSource source : sources) {
source.close();
}
this.out.close();
}
}
import java.util.LinkedList;
import java.util.Queue;
public class RadixSort {
public static void radixSort( int[] array, int maxTimes) {
int i, j, k;
Queue<Integer>[] que = new Queue[10];
for (i = 0; i < 10; i++) {
que[i] = new LinkedList<Integer>();
}
int length = array.length;
for (i = 0; i < maxTimes; i++) {
for (j = 0; j < length; j++) {
k = digit(array[j], i);
que[k].add(array[j]);
}
j = 0;
for (k = 9; k > -1; k--) {
while (!que[k].isEmpty()) {
array[j++] = que[k].remove();
}
}
}
}
public static int getMaxTime( int[] array) {
int max = 0;
for (int i = 0; i < array.length; i++) {
if (max < array[i]) {
max = array[i];
}
}
int times = 0;
while (max > 0) {
max /= 10;
times++;
}
return times;
}
public static int digit(int data, int m) {
int i, d;
if (m == 0) {
return data % 10;
}
d = 10;
for (i = 1; i < m; i++) {
d = d * 10;
}
return (data / d) % 10;
}
}