分页读取文件内容

方法一:简单易懂

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

public class PagingReadFile {

    /** 记录分页次数 */
    public static int sortCount = 0;
    /** 每页读取的条数 */
    public final static int PAGING_COUNT = 100;

    /**
     * 读取文件数据
     */
    public static void pagingReadFileData(){

        int totalCount = getTotalCount();
        // 记录是否是否读到文件末尾,用于判断当最后一页数量不满足100条时也进行业务处理
        int count = 0;
        List<String> stringList = new ArrayList();
        String temp = "";

        try (BufferedReader reader =
                     new BufferedReader(
                             new InputStreamReader(
                                     new FileInputStream(new File("D:\\Users\\Desktop\\paging.txt"))
                                     , "UTF-8"))) {
            while ((temp = reader.readLine()) != null) {
                count++;
                stringList.add(temp);
                // 满足每页读取条数或者当最后一页数量不能满足固定条数时都进行业务处理
                if (stringList.size() == PAGING_COUNT || count == totalCount) {
                    printFileContent(stringList);
                    // 处理后清空再次读取
                    stringList.clear();
                }
            }
        } catch (Exception e) {

        }
    }

    /**
     * 获取数据总条数
     */
    public static int getTotalCount() {
        int count = 0;

        try (BufferedReader reader =
                     new BufferedReader(
                             new InputStreamReader(
                                     new FileInputStream(new File("D:\\Users\\Desktop\\paging.txt"))
                                     , "UTF-8"))) {
            while (reader.readLine() != null) {
                count ++;
            }
        } catch (Exception e) {

        }
        return count;
    }

    /**
     * 业务处理
     */
    public static void printFileContent (List<String> stringList) {
        System.out.println("第" + ++sortCount + "页数据:" + stringList);
    }

    /**
     * 写入数据进行测试
     */
    public static void writeDateToFile () {

        try (BufferedWriter writer =
                     new BufferedWriter(
                             new OutputStreamWriter(
                                     new FileOutputStream(new File("D:\\Users\\Desktop\\paging.txt"))
                                     , "UTF-8"))) {
            for (int i = 1; i <= 2022; i++) {
                writer.append(i + "\r\n");
            }
        } catch (Exception e) {

        }
    }
}

方法二:更像是分页的算法

public static void pagingReadFileData1 () {
        
        int totalPage = 0;
        int start = 0;
        int end = 0;
        int currentPage = 0;
        int totalCount = getTotalCount();
        int count = 0;
        if (totalCount % PAGING_COUNT == 0) {
            totalPage = totalCount / PAGING_COUNT;
        } else {
            totalPage = totalCount / PAGING_COUNT + 1;
        }
        
        List<String> stringList = new ArrayList<>();
        String temp = "";
        try (BufferedReader reader =
                     new BufferedReader(
                             new InputStreamReader(
                                     new FileInputStream(new File("D:\\Users\\Desktop\\paging.txt"))
                                     , "UTF-8"))) {
            while ((temp = reader.readLine()) != null) {
                count++;
                start = (currentPage - 1) * PAGING_COUNT;
                end = currentPage * PAGING_COUNT;
                if (start <= count && count <= end) {
                    stringList.add(temp);
                } else {
                    printFileContent(stringList);
                    stringList.clear();
                    totalPage--;
                    currentPage++;
                    stringList.add(temp);
                }
                // 满足每页读取条数或者当最后一页数量不能满足固定条数时都进行业务处理
                if (count == totalCount) {
                    printFileContent(stringList);
                }
            }
        } catch (Exception e) {

        }
    } 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值