Java 读取TXT文本文件采用BufferedReader

采用Java读取txt文本

关键函数

str.split("\t");  //字符串切割
//BuffReader读
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(path)), "GBK"));//UTF-8
while ((str = br.readLine()) != null)  //每次读一行


按对象读取,定义一个类

我读取的文本数据为id,开始时间,结束时间,持续时间,因此定义类对象读取。

public class Demo {
    int id;
    LocalDateTime startTime;
    LocalDateTime endTime;
    double duration;
}

读取txt数据的关键函数

public class test_read {
    static List<List<Demo>> res = new ArrayList<>();
    public static  BufferedReader readOneFile(String path, BufferedReader br) {
        List<String> lines = new ArrayList<String>();
        List<Demo>  demos = new ArrayList<>();
        try{
            String str = null;
            while ((str = br.readLine()) != null)
            {
                String[] parts = str.split("\t");
                //name为我在读取时不需要读取的那一行数据,读者需自行修改
                if (str.contains(name)){
                    stationName.add(str);
                    break;
                }
                Demo demo = new Demo();
                //trim函数去除两边的多余空格
                demo.setId( Integer.parseInt(parts[0].trim()));
                demo.setStartTime(LocalDateTime.parse(parts[1].trim()));
                demo.setEndTime(LocalDateTime.parse(parts[1].trim()));
                demo.setDuration(Double.parseDouble(parts[3].trim()));
                demos.add(demo);
            }
            //一个对象放入
            res.add(demos);
            //递归读取,递归出口
            if (str == null) return br;
			//递归调用,函数返回为文件指针
            br = readOneFile(path, br);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

		//最后函数运行结束,返回文件指针
        return br;
    }

fileToData()调用test_read()函数

代码如下:

    public static void fileToData()
    {

        String path = "文件名.txt";
        BufferedReader br = null;
        List<List<Demo>> res = new ArrayList<>();
        try
        {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(path)), "GBK"));//UTF-8
            String str = null;
            List<Demo> list = new ArrayList<>();
            //开始读取
            while ((str = br.readLine()) != null)
            {
            	//读取时的判断条件,如果包含name,则开始读取
                if (str.contains(name)) {
                    br = readOneFile(path, br);
                }
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                br.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

    }

最后所有数据存放在res里


总结

利用最主要的关键几个函数实现对txt文本的读取。

完整源代码

import java.io.BufferedReader;
import java.io.*;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.*;


public class test_read {
    static List<List<Demo>> res = new ArrayList<>();
    //地名
    static String name = "Beijing";
    static List<String> stationName = new ArrayList<>();
    public static  BufferedReader readOneFile(String path, BufferedReader br) {
        List<String> lines = new ArrayList<String>();
        List<Demo>  demos = new ArrayList<>();
        try{
            String str = null;
            while ((str = br.readLine()) != null)
            {
                String[] parts = str.split("\t");
                if (str.equals("Beijing_Station-To-Sensor8\t6")) {
                    System.out.println("到达最后一个站");
                }
                if (str.contains(name)){
                    stationName.add(str);
                    break;
                }
                Demo demo = new Demo();
                demo.setId( Integer.parseInt(parts[0].trim()));
                demo.setStartTime(LocalDateTime.parse(parts[1].trim()));
                demo.setEndTime(LocalDateTime.parse(parts[1].trim()));
                demo.setDuration(Double.parseDouble(parts[3].trim()));
                demos.add(demo);
            }
            res.add(demos);
            if (str == null) return br;

            br = readOneFile(path, br);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }


        return br;
    }
    public static void fileToData()
    {

        String path = ".txt";
        BufferedReader br = null;
        List<List<Demo>> res = new ArrayList<>();
        try
        {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(path)), "GBK"));//UTF-8

            String str = null;
            List<Demo> list = new ArrayList<>();
            while ((str = br.readLine()) != null)
            {
             //   System.out.println(str + "\n");
                if (str.contains(name)) {
                    stationName.add(str);
                    br = readOneFile(path, br);
                }

            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                br.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

    }
    public static void main(String[] args) {

        fileToData();
        System.out.println(res.size() + "\t" + stationName.size() + "\n");
        for (int i = 0; i < res.size(); i++) {
            if (stationName.get(i).contains("Satelli")) {
                res.remove(i);
                stationName.remove(i);
            }
        }

        int i = 0;
        for(List<Demo> r : res) {
            System.out.println(stationName.get(i++));
            for (Demo demo : r){
                System.out.println(demo.getId() + "\t" + demo.getStartTime()+ "\t" + demo.getEndTime() + "\t" + demo.getDuration() + "\n");
            }
        }
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值