Java读取TXT文本

时间:2014-09-25 11:35:29   收藏:0   阅读:252
public String readTxtFile(String filePath) {
		StringBuffer appInfolistInput = new StringBuffer();
		try {
			String encoding = "UTF8";
			File file = new File(filePath);
			if (file.isFile() && file.exists()) {
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(file), encoding);
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					appInfolistInput.append(lineTxt);
				}
				read.close();
				bufferedReader.close();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
		return appInfolistInput.toString();
	}

 

原文:http://www.cnblogs.com/zhangfei/p/3992191.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!