`

Java读取txt文件

阅读更多
package com.chinauip.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class ReaderFileLine {

	/**
	 * @Description: 将文本读取到List中并返回
	 * @param
	 * @param path -
	 *            文件路径
	 * @param
	 * @return
	 * @return List<String> - 返回读取文件行的集合
	 * @throws
	 */
	public static List<String> getFileContent(String path) {
		List<String> strList = new ArrayList<String>();
		File file = new File(path);
		InputStreamReader read = null;
		BufferedReader reader = null;
		try {
			read = new InputStreamReader(new FileInputStream(file),"utf-8");
			reader = new BufferedReader(read);
			String line;
			while ((line = reader.readLine()) != null) {
				strList.add(line);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (read != null) {
				try {
					read.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
		return strList;
	}

	/**
	 * @Description: 获取指定行的值
	 * @param
	 * @param path -
	 *            文件路径
	 * @param
	 * @param row -
	 *            指定行
	 * @return String - 返回指定行的数据,没有指定行时数据返回空字符串
	 * @throws
	 */
	public static String listFileByRow(String path, Integer row) {
		List<String> strList = getFileContent(path);
		int size = strList.size();
		if (size >= (row - 1))
			return strList.get(row - 1);
		else
			return "";

	}

	/**
	 * @Description: 读取第几行到第几行的值
	 * @param
	 * @param path -
	 *            文件路径
	 * @param
	 * @param startLine -
	 *            开始行
	 * @param
	 * @param endLine -
	 *            结束行
	 * @return List<String> - 返回指定区间的集合
	 * @throws
	 */
	public static List<String> listFileByRegionRow(String path,
			Integer startLine, Integer endLine) {
		List<String> strList = getFileContent(path);
		// 指定区间的值存到regionList
		List<String> regionList = new ArrayList<String>();
		int size = strList.size();
		if (size >= (endLine - 1)) {
			for (int i = startLine; i <= endLine; i++)
				regionList.add(strList.get(i - 1));
		}
		return regionList;
	}

	public static void main(String[] args) {
		int startLine = 8, endLine = 20;
		String col7=listFileByRow("D:\\GL_yeb.asd", 7);
		System.out.println("第" + 7 + "行:"
				+ col7);

		List<String> regionList = listFileByRegionRow("D:\\GL_yeb.asd",
				startLine, endLine);
		if (!regionList.isEmpty()) {
			for (String strLine : regionList) {
				System.out.println("第" + startLine + "行:" + strLine);
				String sql="insert into table("+col7+") values("+strLine+")";//这里是sql语句
				startLine++;
			}
		}
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics