File-实现每读取一行输出当前行,并删除这一行

package com.lt.service.qiyouji.api;
import java.io.*;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestFile {
    public static void main(String[] args) {
        String filePath = "D:/test/file.txt"; // 输入文件路径
        File file = new File(filePath);
        //将读取出来的数据存入list集合、一个备份、一个正常运行
        while (true) {
            try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
                String firstLine = br.readLine();
                if (firstLine!=null){
                    System.out.println(firstLine);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            //判断文件是否存在、并且有内容
            if (file.exists() && !(file.exists() && file.length() == 0)) {
                String str = null;
                try {
                    ArrayList<String> list = new ArrayList<>();
                    //BufferedReader是可以按行读取文件
                    FileInputStream inputStream = new FileInputStream(filePath);
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    while ((str = bufferedReader.readLine()) != null) {
                        //如果有空格、去掉空格
                        if (str.contains(" ")) {
                            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
                            Matcher m = p.matcher(str);
                            str = m.replaceAll("");
                        }
                        //如果不是空串、添加到list、然后提交
                        if (!str.equals("")) {
                            list.add(str);
                        }
                    }
                    bufferedReader.close();
                    inputStream.close();
                    //删除第一行、、写回到文本
                    list.remove(0);
                    FileOutputStream fileOutputStream = new FileOutputStream(filePath);
                    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
                    BufferedWriter bw = new BufferedWriter(outputStreamWriter);

                    if (list.size() != 0)
                        for (int i = 0; i < list.size(); i++) {
                            String string = list.get(i);
                            bw.write(string);
                            bw.newLine();
                        }
                    else if (list.size() == 0) {
                        bw.write("");
                    }
                    bw.flush();
                    bw.close();
                    //file.delete();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                break;
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值