Java 读取 txt 文件内容到容器 List
方法一:
一、桌面上准备 DataObject.txt 文件,内容为:
二、打开 Eclipse,编写代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.filechooser.FileSystemView; public class test003 {
public static void main(String[] args) {
FileSystemView fsv = FileSystemView.getFileSystemView();
File com = fsv.getHomeDirectory();
String deskPath = com.getPath();
String filePath = deskPath + "\\" + "DataObject.txt"; List<String> list = new ArrayList<String>();
try {
list = getFileContext( filePath );
for( int i = 0; i < list.size(); i++ ) {
String lineStr = list.get(i);
System.out.println( "lineStr = " + lineStr );
}
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 获取txt文件内容, 并按行放入list中
*/
public static List<String> getFileContext( String path ) {
FileReader fileReader = null;
BufferedReader bufferedReader = null;
List<String> list = new ArrayList<String>();
String str = "";
try {
fileReader = new FileReader( path );
bufferedReader = new BufferedReader( fileReader );
while( (str = bufferedReader.readLine()) != null ) {
if( str.trim().length() > 2 ) {
list.add( str );
}
}
} catch ( Exception e ) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (Exception e2) {
e2.printStackTrace();
} try {
if (fileReader != null) {
fileReader.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return list;
}
}
三、执行结果为:
方法二:
一、桌面上准备 DataObject.txt 文件,内容为:
二、打开 Eclipse,编写代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import javax.swing.filechooser.FileSystemView; public class test003 {
public static void main(String[] args) {
FileSystemView fsv = FileSystemView.getFileSystemView();
File com = fsv.getHomeDirectory();
String deskPath = com.getPath();
String filePath = deskPath + "\\" + "DataObject.txt"; List<String> list = new ArrayList<String>();
try {
list = readFileToList( filePath );
for( int i = 0; i < list.size(); i++ ) {
String lineStr = list.get(i);
System.out.println( "lineStr = " + lineStr );
}
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 读取文件内容到容器List
* @param fileName 文件名称
*/
public static List<String> readFileToList( String fileName ) {
List<String> list = new ArrayList<String>();
BufferedReader br = null;
try {
String line = null;
br = new BufferedReader( new UnicodeReader( new FileInputStream(fileName), Charset.defaultCharset().name()) );
while ( (line = br.readLine()) != null ) {
list.add(line);
}
} catch ( IOException e ) {
e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
} return list;
}
}
UnicodeReader 类:
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PushbackInputStream;
import java.io.Reader; public class UnicodeReader extends Reader {
PushbackInputStream internalIn;
InputStreamReader internalIn2 = null;
String defaultEnc; private static final int BOM_SIZE = 4; public UnicodeReader(InputStream in, String defaultEnc) {
internalIn = new PushbackInputStream(in, BOM_SIZE);
this.defaultEnc = defaultEnc;
} public String getDefaultEncoding() {
return defaultEnc;
} public String getEncoding() {
if (internalIn2 == null) return null;
return internalIn2.getEncoding();
} protected void init() throws IOException {
if (internalIn2 != null) return; String encoding;
byte bom[] = new byte[BOM_SIZE];
int n, unread;
n = internalIn.read(bom, 0, bom.length); if ( (bom[0] == (byte)0x00) && (bom[1] == (byte)0x00) &&
(bom[2] == (byte)0xFE) && (bom[3] == (byte)0xFF) ) {
encoding = "UTF-32BE";
unread = n - 4;
} else if ( (bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) &&
(bom[2] == (byte)0x00) && (bom[3] == (byte)0x00) ) {
encoding = "UTF-32LE";
unread = n - 4;
} else if ( (bom[0] == (byte)0xEF) && (bom[1] == (byte)0xBB) &&
(bom[2] == (byte)0xBF) ) {
encoding = "UTF-8";
unread = n - 3;
} else if ( (bom[0] == (byte)0xFE) && (bom[1] == (byte)0xFF) ) {
encoding = "UTF-16BE";
unread = n - 2;
} else if ( (bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) ) {
encoding = "UTF-16LE";
unread = n - 2;
} else {
encoding = defaultEnc;
unread = n;
} if (unread > 0) internalIn.unread(bom, (n - unread), unread); if (encoding == null) {
internalIn2 = new InputStreamReader(internalIn);
} else {
internalIn2 = new InputStreamReader(internalIn, encoding);
}
} public void close() throws IOException {
init();
internalIn2.close();
} public int read(char[] cbuf, int off, int len) throws IOException {
init();
return internalIn2.read(cbuf, off, len);
}
}
三、执行结果为:
Java 读取 txt 文件内容到容器 List的更多相关文章
- java读取txt文件内容
package read; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public ...
- JAVA 读取txt文件内容
原文地址https://www.cnblogs.com/xing901022/p/3933417.html 通常,我们可以直接通过文件流来读取txt文件的内容,但有时可能会出现乱码!此时只要设置一下文 ...
- java 读取TXT文件的方法
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- java读取TXT文件的方法
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- Java读取txt文件信息并操作。
一.java读取txt文件内容 import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.Fi ...
- JAVA获取txt文件内容
JAVA 读取txt文件内容 通常,我们可以直接通过文件流来读取txt文件的内容,但有时可能会出现乱码!此时只要设置一下文件字符编码即可. public class txttest { /** * 读 ...
- java读取txt文件的2中方法---并将内容(每一行以固定的字符分割切成2段)存到map中去
#java读取txt文件的第一种方法 /** * 方法:readTxt * 功能:读取txt文件并把txt文件的内容---每一行作为一个字符串加入到List中去 * 参数:txt文件的地址 * 返回: ...
- Java读取txt文件
package com.loongtao.general.crawler.slave.utils; import java.io.BufferedReader; import java.io.File ...
- Java读取txt文件、excel文件的方法
Java读取txt文件.excel文件的方法 1.读取txt文件 public static String getFileContent(String filePath,String charset) ...
随机推荐
- [转] 设置div的overflow:scroll,但是在手机上滑动的时候有点卡顿
设置div的overflow:scroll,但是在手机上滑动的时候有点卡顿,所以在这个div上加一个css: -webkit-overflow-scrolling : touch; 在苹果手机上使用- ...
- [转] 用webpack的CommonsChunkPlugin提取公共代码的3种方式
方式一,传入字符串参数 new webpack.optimize.CommonsChunkPlugin(‘common.js’), // 默认会把所有入口节点的公共代码提取出来,生成一个common. ...
- [转] 谈谈JS中的函数节流
函数节流的目的 从字面上就可以理解,函数节流就是用来节流函数从而一定程度上优化性能的.例如,DOM 操作比起非DOM 交互需要更多的内存和CPU 时间.连续尝试进行过多的DOM 相关操作可能会导致浏览 ...
- [转]maven全局配置文件settings.xml详解
概要 settings.xml有什么用? 如果在Eclipse中使用过Maven插件,想必会有这个经验:配置settings.xml文件的路径. Paste_Image.png settings.xm ...
- 【原创】c# socket 粘包 其实。。。
文章内容有错,请直接关闭~~~不要看了.丢人. private static Dictionary<string, Packet> cache = new Dictionary<st ...
- pycharm-professional-2017.1.1.exe专业版激活方法
pycharm 2017.1专业版破解码 1.在server选项里边输入 http://elporfirio.com:1017/就可以了. BIG3CLIK6F-eyJsaWNlbnNlSWQiOiJ ...
- VMware安装操作系统提示 " Intel VT-x 处于禁用状态"解决方法
VMWARE WORKSTATION 在安装64为操作系统(kali)报错,报错内容为:“已将该虚拟机配置为使用 64 位客户机操作系统.但是,无法执行 64 位操作. 此主机支持 Intel VT- ...
- asp grid 增加和删除行数据
<table border="0" cellpadding="0" cellspacing="0" style="width ...
- 【转】Linux 虚拟内存和物理内存的理解
http://www.cnblogs.com/dyllove98/archive/2013/06/12/3132940.html 首先,让我们看下虚拟内存: 第一层理解 1. 每个进程 ...
- Shiro笔记(三)shiroFilter拦截器配置原则
参考: http://blog.csdn.net/yaowanpengliferay/article/details/17281341