程序代码如下,
问题如下:
1、点击“open”按钮,打开一个文本文件,文本内容显示在txta(文本框)中,但是很奇怪,txta中只显示打开的文本文件内容的倒数第二行,这是为什么???
2、点击“保存”按钮,把txta(文本框)中的几行字符保存文本文件,假设是aa.txt,但是当我打开刚保存的aa.txt文件时,发现只能显示一行,回车换行以字符“■”代替,这是为什么?
以上两个问题该如何解决?望个路高手不吝赐教!!!小弟在此先感谢了!!!
祝各位通道中人:新年愉快!!!import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;/**
 * @author liyongjie
 *
 */public class JFileDialog { /**
 * 
 */
JFrame frm=null;
JTextArea txta=null;
JScrollPane jsp=null;
JPanel btnp=null;
JButton openbtn=null,savebtn=null,exitbtn=null;

public JFileDialog() {
// TODO 自动生成构造函数存根
frm=new JFrame("文件对话框测试!");
Container cp=frm.getContentPane();
frm.setBounds(300,200,500,400);

txta=new JTextArea(16,10);
txta.setAutoscrolls(true);
jsp=new JScrollPane(txta);
openbtn=new JButton("open");
savebtn=new JButton("save");
exitbtn=new JButton("exit"); btnp=new JPanel();
btnp.add(openbtn);
btnp.add(savebtn);
btnp.add(exitbtn);
btnp.setBorder(BorderFactory.createTitledBorder("open file"));

cp.add(jsp,BorderLayout.NORTH);
cp.add(btnp);

openbtn.addActionListener(new BtnLis());
savebtn.addActionListener(new BtnLis());
exitbtn.addActionListener(new BtnLis());

frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class BtnLis implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if((JButton)e.getSource()==openbtn)//打开文件
{
JFileChooser fileChooser=new JFileChooser();
int result=fileChooser.showOpenDialog(frm);
String currentFilePath;
String fileName;
if(result==JFileChooser.APPROVE_OPTION);
{
currentFilePath=fileChooser.getSelectedFile().getPath();//包括文件路径和文件全名
fileName=fileChooser.getSelectedFile().getName();
txta.setText("you select file name is:"+fileName+"\n"+"the file path is:"+currentFilePath+"\n");
try
{
FileReader fileReader=new FileReader(currentFilePath);
BufferedReader buf=new BufferedReader(fileReader);
String str;
while((str=buf.readLine())!=null)
{
txta.append(buf.readLine());
}
fileReader.close();
buf.close();
}
catch(IOException ioe)
{
txta.setText("文件读取失败");
}
}
}
else if((JButton)e.getSource()==savebtn)//保存文件
{
File file;
JFileChooser fileChooser=new JFileChooser();
String filePath;
String fileName;
int result=fileChooser.showSaveDialog(frm);
if(result==JFileChooser.APPROVE_OPTION)
{
file=fileChooser.getSelectedFile();
filePath=fileChooser.getSelectedFile().getPath();
fileName=file.getName();
try
{
FileWriter fileWriter=new FileWriter(filePath,true);
fileWriter.write(txta.getText());
fileWriter.close();
}
catch(IOException ioe)
{
txta.setText("文件读写失败!");
}
}
}
else if((JButton)e.getSource()==exitbtn)//退出
{
System.exit(0);
}
}
} /**
 * @param args
 */
public static void main(String[] args) {
// TODO 自动生成方法存根
new JFileDialog(); }}

解决方案 »

  1.   

    楼主太粗心了点,把:
    while((str=buf.readLine())!=null){
    txta.append(buf.readLine());
    }
    中的txta.append(buf.readLine())改成txta.append(buf.readLine()+"\n");
    就OK了.
      

  2.   

    加上了也不行,也只能读取倒数第二行和NULL啊
      

  3.   

    while((str=buf.readLine())!=null){
    txta.append(buf.readLine());
    }
    中的txta.append(buf.readLine())改成txta.append(str+"\r\n");
    你那样相当于读了两行,但只写入一行。
      

  4.   

    就是把txta.append(buf.readLine())改成txta.append(str+"\r\n");啊
      

  5.   

    你在while的条件读到字符串里了,下边用字符串写
      

  6.   

    晕,我贴代码的时候贴错了,
    如shan1119所说...