txt文件行是空的处理方式

本文介绍了一个使用Java实现的文件读取程序,该程序能够从指定路径读取文本文件内容,并通过处理去除空行后将有效信息存入List中。文章展示了如何逐行读取文件、处理并存储数据的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 *@Author:jilongliang
 *@Date:2013-4-17
 */
public class Test11 {

	public static void main(String args[]) throws Exception {
		String path="D:\\DATA\\ttt.txt";
//		InputStream in = Test11.class.getResourceAsStream(path);
//		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//		String line = readLine(2, reader);// 读取第2行
//		System.out.println(line);
//		reader.close();
		List<String> list=getTxtContent(path);
		for (int j=3;j<list.size();j++) { ///
			System.out.println("第一条:"+list.get(0) );
			System.out.println("时间:"+list.get(2) );
			System.out.println("联系电话"+list.get(j) );
		}
	}

	private static String readLine(int lineNumber, BufferedReader reader)
			throws Exception {
		String line = "";
		int i = 0;
		while (i < lineNumber) {
			line = reader.readLine();
			i++;
		}
		return line;
	}
	/**
	 * 獲取文本內容
	 * @param path文件路徑
	 * 该方法是对文件的哪一行有空的就处理掉,放入一个List里面然后再读第一条数据和时间到
	 * 一个表的相应字段里面去,而且内容和时间是相同的,联系方式不一样.
	 * 
	 * @return
	 */
	public static List<String> getTxtContent(String path){
		List<String> content=new ArrayList<String>(); 
		BufferedReader reader = null;
		String Line = "";
		try {
			reader = new BufferedReader(new FileReader(path));
			while ((Line = reader.readLine()) != null) {
				Line=Line.trim();//去掉空行
 
				if(!Line.equals(""))//不是空的就添加
				content.add(Line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return content;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值