Java 实现读取文件指定行
来源:互联网 发布:php cli 获取服务器ip 编辑:程序博客网 时间:2024/05/19 14:56
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;
/**
*
* @Title: ReaderFileLine.java
* @Package com.io
* @Description: 读取文件的指定行
* @author zxt
* @date 2011-12-1 上午09:32:37
* @version V1.0
*/
public class ReaderFileLine {
/**
*
* @Title: getTxtContent
* @Description: 将文本读取到List中并返回
* @param @param path - 文件路径
* @param @return
* @return List<String> - 返回读取文件行的集合
* @throws
*/
public static List<String> getFileContent(String path) {
List<String> strList = new ArrayList<String>();
try {
File file = new File(path);
InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader 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();
}
return strList;
}
/**
*
* @Title: listFileByRow
* @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 "";
}
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 = 17, endLine = 53;
System.out.println("第" + startLine + "行:" + listFileByRow("D:\\f5-log\\access.log", startLine));
List<String> regionList = listFileByRegionRow("D:\\f5-log\\access.log", startLine, endLine);
if(!regionList.isEmpty()) {
for(String strLine : regionList) {
System.out.println("第" + startLine + "行:" + strLine);
startLine ++;
}
}
}
}
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;
/**
*
* @Title: ReaderFileLine.java
* @Package com.io
* @Description: 读取文件的指定行
* @author zxt
* @date 2011-12-1 上午09:32:37
* @version V1.0
*/
public class ReaderFileLine {
/**
*
* @Title: getTxtContent
* @Description: 将文本读取到List中并返回
* @param @param path - 文件路径
* @param @return
* @return List<String> - 返回读取文件行的集合
* @throws
*/
public static List<String> getFileContent(String path) {
List<String> strList = new ArrayList<String>();
try {
File file = new File(path);
InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader 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();
}
return strList;
}
/**
*
* @Title: listFileByRow
* @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 "";
}
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 = 17, endLine = 53;
System.out.println("第" + startLine + "行:" + listFileByRow("D:\\f5-log\\access.log", startLine));
List<String> regionList = listFileByRegionRow("D:\\f5-log\\access.log", startLine, endLine);
if(!regionList.isEmpty()) {
for(String strLine : regionList) {
System.out.println("第" + startLine + "行:" + strLine);
startLine ++;
}
}
}
}
1 0
- Java中实现读取文件指定行
- Java中实现读取文件指定行
- Java 实现读取文件指定行
- Java如何实现按指定行读取文件
- java实现读取指定文件内容
- java 读取文件指定行的内容
- 读取文件指定行
- java 代码读取指定文件
- java 读取指定行
- Java读取CSV文件指定行的值
- 使用Java按指定的行读取文件
- vb 读取文件指定行
- 读取文件指定行linecache
- Java按指定行数读取文件
- Java按指定行数读取文件
- Java按指定行数读取文件
- Java读取指定文件的内容
- java读取目录下面指定文件
- Collections.sort()方法对象排序
- CI框架搜索分页
- 使用Nexus搭建Maven私服
- 【基础算法】嵌套矩形
- 使用Quagga测试IPv4/IPv6 BGP双栈动态路由(by quqi99)
- Java 实现读取文件指定行
- 容斥原理 —— 不重不漏的计数
- Android BGABadgeView:新消息/未接来电/未读消息/新通知圆球红点提示(1)
- MyBatis学习总结(七)缓存
- 有容云:DevOps 的起点-入手微型数据中心(下)
- jQuery select操作
- 重新认识HashMap(in JDK1.8)
- test对拍板子(但并不理解原理)
- 递推 HDOJ2045