import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
public class LargeMappedFiles {
/**
* Big data sorting and merging
*
* @param args
*/
public static void main(String[] args) throws IOException {
//Path to write file
String filePath = "D://456";
// Path to split file
String sqlitFilePath = "D://456//123";
//Number of data
int CountNumbers=10000000;
//The number of sub-files
int CountFile=10;
//Precision
int countAccuracy=30*CountFile;
long startNumber=System.currentTimeMillis();
//Write big data file
WriteData(filePath,CountNumbers);
System.out.println("Storage completed");
// Split the big data file into ten other small files
sqlitFileDate(filePath, sqlitFilePath,CountFile);
System.out.println("File cutting completed!");
// Sort the data of each file
singleFileDataSort(sqlitFilePath,CountFile);
System.out.println("Each sub-file is sorted!");
//Accuracy adjustment, comparison and integration of ten file data
deathDataFile(filePath,sqlitFilePath,countAccuracy,CountFile);
System.out.println("Integration completed");
long stopNumber=System.currentTimeMillis();
System.out.println("Time consuming"+(stopNumber-startNumber)/1000+"milliseconds");
}
//Write big data file
public static void WriteData(String path,int CountNumbers) throws IOException {
path = path + "//12114.txt";
FileWriter fs = new FileWriter(path);
BufferedWriter fw=new BufferedWriter(fs);
for (int i = 0; i < CountNumbers; i++) {
fw.write(new Random().nextInt(Integer.MAX_VALUE) + "/r/n");
}
fw.close();
fs.close();
}
// Split the big data file into ten other small files
public static void sqlitFileDate(String filepath, String sqlitPath,
int CountFile) throws IOException {
FileWriter fs = null;
BufferedWriter fw=null;
FileReader fr = new FileReader(filepath + "//12114.txt");
BufferedReader br = new BufferedReader(fr); // Read and get the entire line of data
int i = 1;
LinkedList WriterLists=new LinkedList(); //Initialize the file stream object collection
LinkedList fwLists=new LinkedList();
for (int j = 1; j <= CountFile; j++) {
//Declare object
fs = new FileWriter(sqlitPath + "//12" + j + ".txt",false);
fw=new BufferedWriter(fs);
//Load the object into the collection
WriterLists.add(fs);
fwLists.add(fw);
}
//Determine whether there is still data to return in the file stream
while (br.ready()) {
int count=1;//Initialize the first file stream
for (Iterator iterator = fwLists.iterator(); iterator.hasNext();) {
BufferedWriter type = (BufferedWriter) iterator.next();
if(i==count)//Determine which file stream it is the turn to write data to
{
//Write data, jump out, proceed to the next file stream, and write the next data
type.write(br.readLine() + "/r/n");
break;
}
count++;
}
//Determine whether the last file stream has been reached
if (i >= CountFile) {
i = 1;
} else
i++;
}
br.close();
fr.close();
for (Iterator iterator = fwLists.iterator(); iterator.hasNext();) {
BufferedWriter object = (BufferedWriter) iterator.next();
object.close();
}
//Traverse and close all sub-file streams
for (Iterator iterator = WriterLists.iterator(); iterator.hasNext();) {
FileWriter object = (FileWriter) iterator.next();
object.close();
}
}
// Sort the data of each file
public static void singleFileDataSort(String path1,int CountFile) throws IOException {
LinkedList nums = null;
for (int i = 1; i <= CountFile; i++) {
nums = new LinkedList();
String path = path1 + "//12" + i + ".txt";
try {
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
while (br.ready()) {
// Add the read single data to the collection
nums.add(Integer.parseInt(br.readLine()));
}
// Sort the collection
Collections.sort(nums);
//Write the sorted data into the source file
numberSort(nums, path);
br.close();
fr.close();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Sort each file data and then write it to the source file
public static void numberSort(LinkedList list, String path) {
try {
FileWriter fs = new FileWriter(path);
BufferedWriter fw=new BufferedWriter(fs);
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
fw.write(object + "/r/n");
}
fw.close();
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//Final integration of file data (accuracy adjustment)
public static void deathDataFile(String filepath, String sqlitFilePath1,
int countAccuracy, int CountFile) throws IOException {
LinkedList nums = new LinkedList(); //Add data and sort
Object temp = null; // Record the last number left in each sorting
boolean ispass = false;
LinkedList ispasses = null; //Record the status information of the data file
FileWriter fs = new FileWriter(filepath + "//Sort.txt", false); //Create a file stream for integrated data writing
BufferedWriter bw=new BufferedWriter(fs);
FileReader fr = null; //Declare reading file stream
BufferedReader br = null; //Declare BufferedReader
LinkedList WriterLists = new LinkedList(); // Initialize the file stream object collection
LinkedList WriterListFile = new LinkedList();
for (int j = 1; j <= CountFile; j++) {
// Declare the object and open all sub-file streams to access the data of all sub-files
fr = new FileReader(sqlitFilePath1 + "//12" + j + ".txt");
//Open all BufferedReaders to facilitate reading of the entire line next time
br = new BufferedReader(fr);
//Load all FileReader objects into the collection
WriterListFile.add(fr);
//Load all BufferedReader objects into the collection
WriterLists.add(br);
}
for (;;) {
// Store the data status of the ten source files into a collection to facilitate subsequent judgment.
ispasses = new LinkedList();
// Read individual data from ten source files respectively
for (Iterator iterator = WriterLists.iterator(); iterator.hasNext();) {
BufferedReader object = (BufferedReader) iterator.next();
Object obj = null;
while (object.ready()) {
//Add data for each file stream
nums.add(Integer.parseInt(object.readLine().toString()));
break;
}
if (object.ready() == false)
ispasses.add("true"); //Save the data status in each file into the collection
}
// Determine whether it is the first time to come in
if (nums.size() % countAccuracy == 0 && ispass == false) {
// Sort the collection
Collections.sort(nums);
//Receive the largest data, and write other data to the total sort file
temp = numberSortData(nums, filepath, false, countAccuracy, bw);
//Reinitialize the collection
nums = new LinkedList();
//Add the remaining data from the previous set of comparisons
nums.add(temp);
ispass = true;
//Record the data quantity of the source file for the next traversal
continue;
}
if (ispass) {
if (nums.size() % countAccuracy == 1 && nums.size() > 1) {
// Sort the collection
Collections.sort(nums);
//Receive the largest data, and write other data to the total sort file
temp = numberSortData(nums, filepath, true, countAccuracy,
bw);
nums = new LinkedList();
nums.add(temp);
continue;
}
}
//Record the location of the next set of data
// Determine whether all ten files have no data
if (ispasses.size() == CountFile) {
Collections.sort(nums);
temp = numberSortData(nums, filepath, true, countAccuracy, bw);
nums = new LinkedList();
break;
}
}
bw.close();
//Close the write stream
fs.close();
//Close all BufferedReaders
for (Iterator iterator = WriterLists.iterator(); iterator.hasNext();) {
BufferedReader object2 = (BufferedReader) iterator.next();
object2.close();
}
//Close all FileReaders
for (Iterator iterator = WriterListFile.iterator(); iterator.hasNext();) {
FileReader object = (FileReader) iterator.next();
object.close();
}
}
// Sort the data and write it into the final file (precision adjustment)
public static Object numberSortData(LinkedList list, String filePath,
boolean ispass, int countAccuracy,BufferedWriter fs) {
Object temp = 0; //Record the last value
int tempCount = 0; //Record the written data location
try {
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
// Determine whether it is the last number
if (tempCount == list.size() - 1) {
// Judgment that there are less than 100 in the set
if (list.size() < countAccuracy + 1 && ispass) {
temp = null;
} else {
temp = object;
break;
}
}
//Write to data source
fs.write(object + "/r/n");
//The subscript of the recorded data
tempCount++;
}
} catch (IOException e) {
e.printStackTrace();
}
return temp;
}
}