package servlet;
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.UnsupportedEncodingExcep tion;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static String[] getTxtContent(String path)
{
File f = null;
String[] a = null;
try {
a = new String[100000];
f=new File(path);
InputStreamReader read = new InputStreamReader(new FileInputStream(f), "GBK");
BufferedReader reader = new BufferedReader(read);
String line;
int i;
for (i = 0; i < 100000; i++)
{
if ((line = reader.readLine()) != null)
{
a[i] = line;
}
}
} catch (UnsupportedEncodingExcep tion e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return a;
}
public String listTxtByRow1(String path,Integer row)
{
String[] s = getTxtContent(path);
return "第"+row+"行:"+s[row-1];
}
public List listTxtByRow2(String path,Integer start,Integer end)
{
List list =new ArrayList();
String[] s = getTxtContent(path);
for(int i = start;i <= end;i++)
{
list.add(s[i-1]);
}
return list;
}
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.listTxtByRow1("C:/testlog.txt",6));//取出第6行数据
System.out.println("==================取出指定行数=====================");
List list = t.listTxtByRow2("C:/testlog.txt", 2, 5);//取出2-5行数据
for(int i = 0;i
{
System.out.println(list.get(i));
}
} }
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.UnsupportedEncodingExcep
import java.util.ArrayList;
import java.util.List;
public class Test {