采用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");
}
}
}
}