欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 实现将多个有序的日志文件 合并成一个有序的日志文件(RandomAccessFile实现 或者用 IO流实现【BufferedReader,BufferedOutputStream】)

程序员文章站 2022-03-22 09:04:03
...

基于IO流实现【BufferedReader,BufferedOutputStream】:

import java.io.*;

public class FileReaderObject {
	BufferedReader bufferedReader = null;

	public FileReaderObject(String filePath) {
		try {
			this.bufferedReader = new BufferedReader(new FileReader(new File(filePath)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}


	public String read () {
		String content;
		try {
			while ((content = bufferedReader.readLine()) != null) {
				return content;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public void close() {
		try {
			bufferedReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

public class SortObject implements  Comparable<SortObject>{
	String content;
	FileReaderObject rd;

	public SortObject(FileReaderObject rd) {
		this.rd = rd;
		this.content = rd.read();
	}

	public  void next() {
		this.content = rd.read();
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public FileReaderObject getRd() {
		return rd;
	}

	public void setRd(FileReaderObject rd) {
		this.rd = rd;
	}

	@Override
	public int compareTo(SortObject o) {
		if (o.content == null) {//当内容为空的时候,将null放在列表最后
			return  -1;
		}
		if (this.content == null) {//当内容为空的时候,将null放在列表最后
			return  1;
		}
		if (Integer.valueOf(this.content) > Integer.valueOf(o.content)) {
			return 1;
		}
		return  -1;
	}


}


import cn.rojao.util.FileUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortUtill {

	public static void main(String[]args){
		FileReaderObject wrFile1 = new FileReaderObject("D:\\paixu\\a.txt");
		FileReaderObject wrFile2 = new FileReaderObject("D:\\paixu\\b.txt");
		FileReaderObject wrFile3 = new FileReaderObject("D:\\paixu\\c.txt");
		SortObject k1 = new SortObject(wrFile1);
		SortObject k2 = new SortObject(wrFile2);
		SortObject k3 = new SortObject(wrFile3);
		List<SortObject> sortObjectList = new ArrayList<>();
		sortObjectList.add(k1);
		sortObjectList.add(k2);
		sortObjectList.add(k3);
		Collections.sort(sortObjectList);
		FileUtil.createFile("D:\\paixu\\abc.txt");
		OutputStreamWriter write = null;
		BufferedOutputStream writer = null;
		try {
			writer = new BufferedOutputStream(new FileOutputStream(new File("D:\\paixu\\abc.txt")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		sort(writer,sortObjectList);
		wrFile1.close();
		wrFile2.close();
		wrFile3.close();
		try {
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}



	public static void sort(BufferedOutputStream bufferedOutputStream, List<SortObject> sortObjects) {
		while (true) {
			try {
				bufferedOutputStream.write(sortObjects.get(0).getContent().getBytes());
				bufferedOutputStream.write("\r\n".getBytes());
				bufferedOutputStream.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			sortObjects.get(0).next();
			Collections.sort(sortObjects);
			if (sortObjects.get(0).getContent() == null || "".equals(sortObjects.get(0).getContent())) {
				break;
			}
		}
	}
}

基于RandomAccessFile实现:


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Ramdomaccfile {
	RandomAccessFile ramdom = null;

	public Ramdomaccfile(String filePath) {
		try {
			this.ramdom = new RandomAccessFile(filePath,"rw");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void write(String content) {
		try {
			ramdom.seek(ramdom.length());
			ramdom.write(content.getBytes());
			ramdom.write("\r\n".getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String read () {
		String content;
		try {
			while ((content = ramdom.readLine()) != null) {
				String data = new String(content.getBytes("ISO-8859-1"), "utf-8");
				return data;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public void close() {
		try {
			ramdom.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


public class SortObject implements  Comparable<SortObject>{
	String content;
	Ramdomaccfile rd;

	public SortObject(Ramdomaccfile rd) {
		this.rd = rd;
		this.content = rd.read();
	}

	public  void next() {
		this.content = rd.read();
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Ramdomaccfile getRd() {
		return rd;
	}

	public void setRd(Ramdomaccfile rd) {
		this.rd = rd;
	}

	@Override
	public int compareTo(SortObject o) {
		if (o.content == null) {
			return  -1;
		}
		if (this.content == null) {
			return  1;
		}
		if (Integer.valueOf(this.content) > Integer.valueOf(o.content)) {
			return 1;
		}
		return  -1;
	}


}


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortUtill {

	public static void main(String[]args){
		Ramdomaccfile wrFile1 = new Ramdomaccfile("D:\\paixu\\a.txt");
		Ramdomaccfile wrFile2 = new Ramdomaccfile("D:\\paixu\\b.txt");
		Ramdomaccfile wrFile3 = new Ramdomaccfile("D:\\paixu\\c.txt");
		SortObject k1 = new SortObject(wrFile1);
		SortObject k2 = new SortObject(wrFile2);
		SortObject k3 = new SortObject(wrFile3);
		List<SortObject> sortObjectList = new ArrayList<>();
		sortObjectList.add(k1);
		sortObjectList.add(k2);
		sortObjectList.add(k3);
		Collections.sort(sortObjectList);
		Ramdomaccfile openFile = new Ramdomaccfile("D:\\paixu\\abc.txt");
		sort(openFile,sortObjectList);
		wrFile1.close();
		wrFile2.close();
		wrFile3.close();
		openFile.close();
	}



	public static void sort(Ramdomaccfile ramdomaccfile, List<SortObject> sortObjects) {
		while (true) {
			ramdomaccfile.write(sortObjects.get(0).getContent());
			sortObjects.get(0).next();
			Collections.sort(sortObjects);
			if (sortObjects.get(0).getContent() == null || "".equals(sortObjects.get(0).getContent())) {
				break;
			}
		}
	}
}

 

相关标签: 排序