package com.shscn;

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

public class Port {
	/**
	 * 功能:Java读取txt文件的内容 步骤:1:先获得文件句柄 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
	 * 3:读取到输入流后,需要读取生成字节流 4:一行一行的输出。readline()。 备注:需要考虑的是异常情况
	 * @param filePath
	 */
	public static Object[] readTxtFile(String filePath) {
		InputStreamReader read = null;
		BufferedReader bufferedReader = null;
		Object[] result = null;
		try {
			String encoding = "GBK";
			File file = new File(filePath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				read = new InputStreamReader(
						new FileInputStream(file), encoding);// 考虑到编码格式
				bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				ArrayList<String> list = new ArrayList<String> ();
				while ((lineTxt = bufferedReader.readLine()) != null) {
			        list.add (lineTxt);
				}
				result = list.toArray ();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		} finally {
			try {
				if (bufferedReader != null) 
					bufferedReader.close();
				if (read != null) 
					read.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
}
最近下载更多
75431368  LV10 2021年7月16日
马儿爱吃兰  LV10 2021年6月15日
wangshixi2010  LV3 2020年8月21日
wjschm  LV14 2020年4月27日
huaua7676  LV30 2020年1月15日
benyan  LV8 2019年4月29日
阿毛123456  LV13 2019年4月14日
dmzhjg  LV6 2018年8月31日
shiwenliang  LV5 2018年5月30日
byj1987  LV18 2018年5月13日
最近浏览更多
666ing  LV2 2023年9月27日
flyjoe  LV3 2023年7月11日
ericxu1116  LV24 2023年6月26日
2292250314  LV2 2023年5月28日
王培龚  LV4 2023年4月10日
hkxyyz  LV6 2022年11月14日
lilu0226  LV7 2022年7月31日
帅潇潇  LV12 2022年6月13日
Trickster  LV9 2022年5月17日
crosa_Don  LV18 2022年4月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友