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++;
}
}
}
}
分享到:
相关推荐
java读取TXT文件入库,java读取TXT文件入库,java读取TXT文件入库,
java读取txt文件,可以以文件路径构造这个流,:FileInputStream fin = new FileInputStream("d:/test.txt"); 然后使用这个流直接读取到文件,再使用reader构造BufferedReader,按行读取一整行的文本,作为字符串返回...
使用java语言,通过调用FileReader类读取txt文件中的数据。资源是可直接实现的代码。
Java\读取TXT文件.txtJava\读取TXT文件.txt
java读取txt文件代码,轻松掌握java,不成问题
环境:eclipse3.7.2 jre1.6
java 创建txt_JAVA读取TXT文件、新建TXT文件、写入TXT文件
用java读取txt文件 并且格式化输出读取到的信息 用java读取excel的xml格式文件 对信息进行分类 并且格式化输出有效内容
java 读取txt文本文件中的数据并保存到数据库中源代码,假设txt已有格式,并以","分隔。其中的sql包需要自己去微软官网下载。
用Java程序实现对.txt文件的读写。txt文件可作为配置文件,以key-Value形式出现。
JAVA实现创建TXT文件 并执行读写操作
主要介绍了java读取txt文件并输出结果,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
java利用io技术创建文件夹、读取txt文件、写入txt文件(覆盖、不覆盖均有)
JAVA简单的读写文本文件的代码JAVA简单的读写文本文件的代码
JAVA读取TXT文件.pdf
读取一个文件加下的txt文件,读取文件中内容,自动录入数据库