java读取txt文档,并进行分页 30
java读取一个日志文件,在前台要进行分页显示,比如可以查看第20-40行的内容,该怎么做?……...
java读取一个日志文件,在前台要进行分页显示,比如可以查看第20-40行的内容,该怎么做?……
展开
3个回答
展开全部
用IO流,NIO也行。
BufferedReader bufReader = new BufferedReader(new FileReader(new File("C:/test.txt")));
String str="",result="";
int count=0;
while((str=bufReader.readLine())!=null){
if(count>=20||count<40){
result+=str;
}else if (count>40){
break;
}
count++;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用StringTokenizer:
StringTokenizer是java中object类的一个子类,继承自 Enumeration接口。此类允许一个应用程序进入一个令牌(tokens),而且StringTokenizer类用起来StreamTokenizer 类更加简单。StringTokenizer不会区分对象、函数、数组、或引证字符串。
代码:
import java.util.Comparator;
public class AgeComparator implements Comparator<Person>{
public int compare(Person object1, Person object2) {
// TODO Auto-generated method stub
if(object1.getAge() > object2.getAge())
return 1;
else if (object1.getAge() < object2.getAge()){
return -1;
}
else
return 0;
}
}
public class Person {
private String date;
private String name;
private int age;
private String sex;
public String getDate() {
return date;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getSex() {
return sex;
}
public void setDate(String date) {
this.date = date;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSex(String sex) {
this.sex = sex;
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class MyFileReader {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
AgeComparator ac = new AgeComparator();
TreeSet<Person> ts = new TreeSet<Person>(ac);
br = new BufferedReader(new FileReader("a.txt"));
String temp = null;
while ((temp = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(temp);
if(st.countTokens() == 4){
Person person = new Person();
person.setDate(st.nextToken());
person.setName(st.nextToken());
person.setAge(Integer.parseInt(st.nextToken()));
person.setSex(st.nextToken());
ts.add(person);
}
else{
System.out.println("数据格式有问题");
return;
}
}
Iterator<Person> iter = ts.iterator();
bw = new BufferedWriter(new FileWriter("b.txt"));
for(;iter.hasNext();){
Person person = iter.next();
StringBuffer sb = new StringBuffer();
sb.append(person.getDate()+" "+person.getName()+" "+person.getAge()+" "+person.getSex()+"\r\n");
System.out.print(sb.toString());
bw.write(sb.toString());
bw.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
br.close();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
StringTokenizer是java中object类的一个子类,继承自 Enumeration接口。此类允许一个应用程序进入一个令牌(tokens),而且StringTokenizer类用起来StreamTokenizer 类更加简单。StringTokenizer不会区分对象、函数、数组、或引证字符串。
代码:
import java.util.Comparator;
public class AgeComparator implements Comparator<Person>{
public int compare(Person object1, Person object2) {
// TODO Auto-generated method stub
if(object1.getAge() > object2.getAge())
return 1;
else if (object1.getAge() < object2.getAge()){
return -1;
}
else
return 0;
}
}
public class Person {
private String date;
private String name;
private int age;
private String sex;
public String getDate() {
return date;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getSex() {
return sex;
}
public void setDate(String date) {
this.date = date;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSex(String sex) {
this.sex = sex;
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class MyFileReader {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
AgeComparator ac = new AgeComparator();
TreeSet<Person> ts = new TreeSet<Person>(ac);
br = new BufferedReader(new FileReader("a.txt"));
String temp = null;
while ((temp = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(temp);
if(st.countTokens() == 4){
Person person = new Person();
person.setDate(st.nextToken());
person.setName(st.nextToken());
person.setAge(Integer.parseInt(st.nextToken()));
person.setSex(st.nextToken());
ts.add(person);
}
else{
System.out.println("数据格式有问题");
return;
}
}
Iterator<Person> iter = ts.iterator();
bw = new BufferedWriter(new FileWriter("b.txt"));
for(;iter.hasNext();){
Person person = iter.next();
StringBuffer sb = new StringBuffer();
sb.append(person.getDate()+" "+person.getName()+" "+person.getAge()+" "+person.getSex()+"\r\n");
System.out.print(sb.toString());
bw.write(sb.toString());
bw.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
br.close();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询