此文源码主要为应用 Java 读取文本文件内容实例的源代码。若有不足之处,敬请大神指正,不胜感激!

1.读取的文本文件内容以一维数组【LinkedList<String>】的形式返回,源代码如下所示:

     /**
* @function 文本文件操作:读取数据
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java txtRead, 2015-2-2 21:03:46 Exp $
*
* @param filename :文本文件全路径
* @param encodingType :文本文件编码
*
* @return LinkedList<String> 文本文件内容
*/
public LinkedList<String> txtRead(String filename, String encodingType){
LinkedList<String> fileContent = new LinkedList<String>(); try{
File f = new File(filename); if(f.isFile() && f.exists()){
InputStreamReader read = new InputStreamReader(new FileInputStream(f), encodingType);
BufferedReader br = new BufferedReader(read); String rowContent; while((rowContent = br.readLine()) != null){
fileContent.add(rowContent);
} if(br != null){
br.close();
} if(read != null){
read.close();
}
}else{
this.message = "{" + filename + "}为目录或者文件不存在,或文件正在被占用!";
this.logger.error(this.message);
}
}catch(Exception ioe){
this.message = "读取文件 {" + filename + "}内容出错。" + ioe.getMessage();
this.logger.error(this.message);
} return fileContent;
}

读取文本文件内容,返回数据格式:LinkedList<String>

测试:返回一维数组,源代码如下所示:

     /**
* 测试:FileUtils.txtRead(String, String)
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java txtRead, 2015-2-2 22:07:14 Exp $
*
*/
@Test
public void txtRead_row() {
this.fu = new FileUtils();
this.message = "\n\n\n测试:FileUtils.txtRead(String, String)";
this.logger.debug(this.message); try{
this.filename = this.constantslist.PARAFILEPATH.get("TEST") + "source-txtRead.txt";
this.message = "测试文本文件源为:" + this.filename;
this.logger.debug(this.message); LinkedList<String> contentlist = this.fu.txtRead(this.filename, "GB2312");
String fileContent = ""; // 文件内容 if(contentlist.size() > 0){
for(int rowsCount = 1; rowsCount <= contentlist.size()/1000 + 1; rowsCount++){
for(int i = 0; i < contentlist.size(); i++){
fileContent = contentlist.get(i); this.logger.debug(fileContent); if (i == 5) {
return;
}
}
}
}
}catch(Exception ioe){
ioe.printStackTrace();
this.message = ioe.getMessage();
this.logger.error(this.message);
}
}

测试源代码:

2.读取的文本文件内容以二维数据【LinkedList<String[]>】的形式返回(删除了数据左右空格),源代码如下所示:

     /**
* @function 文本文件操作:读取数据
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java txtRead, 2015-2-2 21:03:46 Exp $
*
* @param filename :文本文件全路径
* @param encodingType :文本文件编码
* @param split : 行分隔符
*
* @return LinkedList<String[]> 文本文件内容
*/
public LinkedList<String[]> txtRead(String filename, String encodingType, String split){
LinkedList<String[]> fileContent = new LinkedList<String[]>(); try{
File f = new File(filename); if(f.isFile() && f.exists()){
InputStreamReader read = new InputStreamReader(new FileInputStream(f), encodingType);
BufferedReader br = new BufferedReader(read); String rowContent; while((rowContent = br.readLine()) != null){
String[] content = rowContent.split(split);
String[] rowdata = new String[content.length]; for (int i = 0; i < content.length; i++) {
rowdata[i] = content[i].toString().trim();
} fileContent.add(rowdata);
} if(br != null){
br.close();
} if(read != null){
read.close();
}
}else{
this.message = "{" + filename + "}为目录或者文件不存在,或文件正在被占用!";
this.logger.error(this.message);
}
}catch(Exception ioe){
this.message = "读取文件 {" + filename + "}内容出错。" + ioe.getMessage();
this.logger.error(this.message);
} return fileContent;
}

读取文本文件内容,返回数据格式:LinkedList<String[]>

测试:返回二维数组,源代码如下所示:

     /**
* 测试:FileUtils.txtRead(String, String, String)
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java txtRead, 2015-2-2 22:17:14 Exp $
*
*/
@Test
public void txtRead_rowcontent() {
this.fu = new FileUtils();
this.message = "\n\n\n测试:FileUtils.txtRead(String, String, String)";
this.logger.debug(this.message); try{
this.filename = this.constantslist.PARAFILEPATH.get("TEST") + "source-txtRead.txt";
this.message = "测试文本文件源为:" + this.filename;
this.logger.debug(this.message); LinkedList<String[]> fdata = this.fu.txtRead(this.filename, "GB2312", "\\|"); if(fdata.size() > 0){
for(int rowsCount = 1; rowsCount <= 5; rowsCount++){
String[] arr_row = fdata.get(rowsCount);
String rowdata = ""; for (int i = 0; i < arr_row.length; i++) {
rowdata += arr_row[i] + "\t";
} this.logger.debug(rowdata);
}
}
}catch(Exception ioe){
ioe.printStackTrace();
this.message = ioe.getMessage();
this.logger.error(this.message);
}
}

测试源代码:

3.文本文件内容如下所示:

     01|20130131| |      |        48104|       |       | | |
READ00|长安|1|000001| 18653471415| 11240| 10000|1|1|1
READ04|哈佛|1|000001| 1150000000| 7650| 10000|1|1|1
READ05|武钢|1|000001| 10093779823| 2990| 10000|1|1|1
READ06|东风|1|000001| 2000000000| 3250| 10000|1|1|1
READ07|现代|1|000001| 1007282534| 11540| 10000|1|1|1
READ08|本田|1|000001| 2200000000| 4500| 10000|1|1|1
READ09|猎豹|1|000001| 1926958448| 13210| 10000|1|1|1
READ10|陆风|1|000001| 6423643659| 5250| 10000|1|1|1
READ11|东风|1|000001| 10500000000| 6380| 10000|1|1|1
READ12|华晨|1|000001| 1165READ00| 4200| 10000|1|1|1
READ15|昨天|1|000001| 6849725776| 11540| 10000|1|1|1
READ16|今天|1|000001| 22587602387| 10360| 10000|1|1|1
READ17|山东|1|000001| 3075653888| 2950| 10000|1|1|1
READ18|上海|1|000001| 22755179650| 2990| 10000|1|1|1
READ19|北京|1|000001| 17122048088| 5030| 10000|1|1|1
READ20|天津|1|000001| 2247371832| 2520| 10000|1|1|1
READ21|南京|1|000001| 2139739257| 4720| 10000|1|1|1
READ22|江苏|1|000001| 6436295797| 2250| 10000|1|1|1
READ26|河北|1|000001| 2108552613| 4770| 10000|1|1|1
READ27|海南|1|000001| 5940056200| 3820| 10000|1|1|1
READ28|丽江|1|000001| 70039798886| 7110| 10000|1|1|1
READ29|天界|1|000001| 7022650000| 4130| 10000|1|1|1

文本文件内容:

脚本运行结果:

Java学习-013-文本文件读取实例源代码(两种数据返回格式)-LMLPHP

至此, Java学习-013-文本文件读取实例源代码(两种数据返回格式) 顺利完结,希望此文能够给初学 Java 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

04-20 23:37