读取文件和分页显示

读取和在JTextArea显示文件:

// read file
public File readFileTxt(){
String str=String.valueOf(DevideText.singleObject().text.getText());
try{
if(str==null) throw new Exception();
file=new File(str);
}
catch (Exception e){
e.printStackTrace();
}
return file;
}
//show the file to textarea
public void showFileTxt() throws IOException{
freader1=new FileReader(file);
buf1=new BufferedReader(freader1);
String temp1=null;
for(;startLine<count*pageSize;startLine++){
temp1=buf1.readLine();
tArea.append(temp1);
tArea.append("\n");
}
}

上一页和下一页:

previousBut.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int i;
String temp3="";
tArea.setText("");
FileReader fi = null;
BufferedReader bu = null;
try {
fi = new FileReader(file);
bu=new BufferedReader(fi);
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}

if((--count)>0){
showPageSize();
setButtonState();
for(i=0;i<count*pageSize;i++){
try {

temp3=bu.readLine();
if(count==1) {
tArea.append(temp3);
tArea.append("\n");
}
if(i>=(count-1)*pageSize&&count!=1){
tArea.append(temp3);
tArea.append("\n");
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
}


}

});

nextBut.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int i;
String temp2="";
tArea.setText("");
FileReader fi = null;
BufferedReader bu = null;
try {
fi = new FileReader(file);
bu=new BufferedReader(fi);
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
if((++count)<=totalPage){
showPageSize();
setButtonState();
nextBut.setEnabled(true);
for( i=0;i<count*pageSize;i++){
try {

temp2=bu.readLine();
if(i>=((count-1)*pageSize)){
tArea.append(temp2);
tArea.append("\n");

}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
}
}
});


做了这个后,对io有了进一步的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值