当前位置:文档之家› Java读取txt文件中指定行内容

Java读取txt文件中指定行内容

获取文本内容与读取文本指定行数内容浅析(java)
在此项目中直接套用以前工程中获取文本内容的方法发现一直提示“数组下标越界”,通过分析和查找得出以下心得:
获取文本内容:
private static final String CHART_PATH ="D://data3";
public static void main(String[] args) throws
RowsExceededException,WriteException, BiffException{
try {
readFileByLines(CHART_PATH+".txt");
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void readFileByLines(String fileName) throws
IOException,RowsExceededException,WriteException{
//打开文件
WritableWorkbook book = Workbook.createWorkbook(
new File(CHART_PATH+".xls"));
WritableSheet sheet = book.createSheet("看我", 0);
//读取txt文件内容
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis,"GBK");
BufferedReader reader = null;
try {
reader = new BufferedReader(isr);
String temString = null;
//一次读入一行,以null结束
int i = 0;
while((temString = reader.readLine())!= null){
System.out.println("+++++++"+temString);
String[] str = temString.split(",");
for(int j= 0;j<str.length;j++){
Label label = new Label(j,i,str[j]);
sheet.addCell(label);
// System.out.println(str[j]);
System.out.println("----------"+str[j]);
}
i++;
}
//写入
book.write();
try {
book.close();
} catch (WriteException e) {
// TODO: handle exception
e.printStackTrace();
}
reader.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if(reader!=null){
reader.close();
}
}
}
从以上代码可以看出是每行读取数据然后存入新的数组中,按道理说可以通过类似int a =0;
a =Integer.parseInt(str[0]);
来获取新数组中的第一个元素,但是此时工程会一直报
ng.ArrayIndexOutOfBoundsException: 0,
说明此时不能直接这么从新数组中获取数据
获取某一行文本信息:
为了获取想要的目标数据可以这么做:
package testTxt;
import java.io.*;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// 指定读取的行号
int lineNumber =5;
// 读取文件
File sourceFile = new File("D://data3.txt");
try {
// 读取指定的行
readAppointedLineNumber(sourceFile, lineNumber);
// readAppointedLineNumber(sourceFile, lineNumber);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 读取文件指定行。

static void readAppointedLineNumber(File sourceFile, int lineNumber) throws IOException {
FileReader in = new FileReader(sourceFile);
LineNumberReader reader = new LineNumberReader(in);
String s = "";
if (lineNumber <= 0 || lineNumber > getTotalLines(sourceFile)) { System.exit(0);
}
int lines = 0;
while (s != null) {
lines++;
s = reader.readLine();
if((lines - lineNumber) == 0) {
System.out.println(s+"wo zai zhe");
int a =0;
a =Integer.parseInt(s);
System.out.println(a+"MMMMMMMMMMMMMMMMMMMMM");
System.exit(0);
}
}
reader.close();
in.close();
}
// 文件内容的总行数。

static int getTotalLines(File file) throws IOException {
FileReader in = new FileReader(file);
LineNumberReader reader = new LineNumberReader(in);
String s = reader.readLine();
int lines = 0;
while (s != null) {
lines++;
s = reader.readLine();
if(lines>=2){
if(s!=null){
// System.out.println(s+"LLLLLLLLLL");
}
}
}
reader.close();
in.close();
return lines;
}
}
从上述代码中可以很清楚的看出想要获取某一行的数据,只需要传入此行的lineNumber就行。

相关主题