读取文件和分页显示

读取和在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有了进一步的理解。

你可能感兴趣的:(java)