Paging query text file content

Before, the foreground page for recording system personnel's activities was to query and display all contents in the local log file. When the log file is small, it is good. When the log file reaches a certain scale, it becomes more and more stuck. Therefore, it needs to be read in the background section by section, and view the log record in the form of "page turning" in the foreground

Because I have used pagehelper before and read some of its source code, I decided to imitate pagehelper to write a paging query function

Main ideas:

  • Read return by fixed number of lines,
    For example, read 10 lines per page,
    On the first page, read the number of lines from 1 to 1 multiplied by 10;
    On page 2, read the cut-off line bits from the previous page: 11 line bits, and read the contents of 11 to 20 line bits again
  • Build a paging model object, including some attributes related to function business

First, the paging model object


package com.allstargh.ssm.pojo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Paging text objects < br >
 * Property: <br>
 * Current page. < br >
 * Total pages. < br >
 * Text content. < br >
 * Whether there is still a previous page. < br >
 * Is there another page left. < br >
 * 
 * @author admin
 *
 */
public class PagingText {
    /**
     * Starting point: 0
     */
    private Integer currentPage;

    /**
     * Count from 0
     */
    private Integer totalPages;

    private String[] textContent;
    private Boolean isPrevious;
    private Boolean isNext;

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public String[] getTextContent() {
        return textContent;
    }

    public void setTextContent(String[] textContent) {
        this.textContent = textContent;
    }

    public Boolean getIsPrevious() {
        return isPrevious;
    }

    public void setIsPrevious(Boolean isPrevious) {
        this.isPrevious = isPrevious;
    }

    public Boolean getIsNext() {
        return isNext;
    }

    public void setIsNext(Boolean isNext) {
        this.isNext = isNext;
    }

    @Override
    public String toString() {
        return "PagingText [currentPage=" + currentPage + ", totalPages=" + totalPages + ", textContent="
                + Arrays.toString(textContent) + ", isPrevious=" + isPrevious + ", isNext=" + isNext + "]";
    }

}


Model processing

packaging is the main package method



package com.allstargh.ssm.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.allstargh.ssm.pojo.PagingText;

/**
 * Read data from text file page by page
 * 
 * <b>Here, number 0 is the first in reality</b>
 * 
 * @author admin
 *
 */
public class SegmentReadText {
    /**
     * The number of lines read from the file at a time, < br >
     * The effect of "paging" can be achieved by taking every index * line "count as the starting line
     */
    public static final Integer LINE_COUNT = 10;

    /**
     * File default encoding
     */
    public static final String FILE_ENCODING = "UTF-8";

    /**
     * Unique identification of the file, used to record which line of the file is read from
     */
    private Map<String, Integer> counterMap = Collections.synchronizedMap(new HashMap<String, Integer>());

    /**
     * Set the number of lines read from the beginning of the file < br >
     * <b>msgKey The default value must be null, not an empty string, otherwise the data will always be the same</b>
     * 
     * @param msgKey Unique identification of the file
     * @param offset Position of the number of rows to start reading
     */
    public void setupBeginLines(String msgKey, int offset) {
        counterMap.put(msgKey, offset);
    }

    /**
     * Read a fixed number of lines from a file at a time
     * 
     * @param msgKey   File unique identification
     * @param filePath File path
     * @return List Read file contents
     */
    public List<Map<Integer, String>> readRowsRecords(String msgKey, String filePath) {
        List<Map<Integer, String>> dataList = new ArrayList<>();

        int line = 0;

        if (counterMap.get(msgKey) == null) {
            counterMap.put(msgKey, line);
        } else {
            line = counterMap.get(msgKey);
        }

        try {
            File file = new File(filePath);

            if (file.isFile() && file.exists()) {// Judge whether the file exists
                // Considering the encoding format
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), FILE_ENCODING);
                BufferedReader buffer = new BufferedReader(read);
                String lineText = null;

                int index = 1;

                while ((lineText = buffer.readLine()) != null) {
                    // Every read starts from the last row bit read
                    if (index > line) {
                        Map<Integer, String> data = new HashMap<>();

                        data.put(index, lineText);

                        dataList.add(data);

                        // How many rows to read at a time
                        if ((index - line) == LINE_COUNT) {
                            line = index;
                            break;
                        }
                    }

                    index++;
                }

                /* This indicates that the file has been read. Insert a read mark. File.rename to (file) is used to determine whether the current file is written or occupied by other programs */
                if (lineText == null && file.renameTo(file)) {
                    Map<Integer, String> data = new HashMap<>();

                    data.put(-1, "It's at the end of the text");

                    dataList.add(data);

                    line = index;

                }

                read.close();
                buffer.close();
            } else {
                System.err.println("Specified file not found");
            }
        } catch (IOException e) {
            System.err.println("Exception reading file");
            e.printStackTrace();
        } finally {
            // Record which line position of the file to read next time
            counterMap.put(msgKey, line);
        }

        return dataList;
    }

    /**
     * Get text data according to file location and page number < br >
     * Page 0 is the first page
     * 
     * @param filePath
     * @param index    Specified page
     * @return
     */
    public List<Map<Integer, String>> getTextData(String filePath, Integer index) {
        /* msgKey The default value must be null, not an empty string, otherwise the data will always be the same */
        setupBeginLines(null, index * LINE_COUNT);

        List<Map<Integer, String>> list = readRowsRecords(null, filePath);
        System.err.println("list.size: " + list.size());

        for (Map<Integer, String> map : list) {
            for (Map.Entry<Integer, String> ele : map.entrySet()) {
                // System.out.println(ele.getKey() + " , " + ele.getValue());
            }
        }

        return list;
    }

    /**
     * Count total lines of text file content
     * 
     * @param filePath
     * @return
     */
    public Integer countTextLines(String filePath) {
        int lines = 0;

        try {
            File file = new File(filePath);

            if (file.exists()) {
                long fileLength = file.length();

                LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(file));

                lineNumberReader.skip(fileLength);

                lines = lineNumberReader.getLineNumber();
                System.out.println("Total number of lines : " + lines);

                lineNumberReader.close();
            } else {
                System.out.println("File does't exists");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return lines;
    }

    /**
     * Refining data < br >
     * In order to better project the text content on the foreground page, we need to convert the content extraction into string array
     * 
     * @param list
     * @return
     */
    public String[] refineData(List<Map<Integer, String>> list) {
        String value = null;

        for (Map<Integer, String> map : list) {
            for (Map.Entry<Integer, String> ele : map.entrySet()) {
                // System.out.println(ele.getKey() + " , " + ele.getValue());
                value += ele.getValue();
            }
        }

        // System.err.println(value);
        String[] split = value.split("\n|\r");

        for (int i = 0; i < split.length; i++) {
            // System.err.println(split[i]);
        }

        return split;
    }

    /**
     * encapsulation
     * 
     * @param filePath
     * @param index
     * @return
     */
    public PagingText packaging(String filePath, Integer index) {
        PagingText text = new PagingText();

        List<Map<Integer, String>> list = getTextData(filePath, index);

        String[] data = refineData(list);

        Integer lines = countTextLines(filePath);

        int totalPages = lines / LINE_COUNT;

        text.setTotalPages(totalPages);

        if (index >= totalPages && totalPages > 0) {
            text.setIsNext(false);
            text.setIsPrevious(true);

        } else if (index < totalPages && index > 0) {
            text.setIsNext(true);
            text.setIsPrevious(true);

        } else if (index == 0 && totalPages > 0) {
            text.setIsNext(true);
            text.setIsPrevious(false);

        } else if (totalPages == 0) {
            text.setIsNext(false);
            text.setIsPrevious(false);

        }

        text.setTextContent(data);
        text.setCurrentPage(index);

        return text;
    }

}




Tags: Java encoding

Posted on Sat, 09 Nov 2019 06:18:06 -0800 by dreamline