我毕业设计中有一部分是查询的功能,就是我导入一个txt文档,里面是查询的字段内容,当我点击查询后我是通过JOptionPane.showMessageDialog()来显示查询的内容的,现在我想把这些显示的内容通过一个导出按钮导出到txt文档里,该怎么写啊?(附代码如下)
import java.awt.*;
import java.awt.event.*;
import java.awt.color.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;
import java.text.*;
 
 class Batch extends JFrame implements ActionListener,KeyListener{
    JButton insertB  = new JButton("导入");
JButton selectB1 = new JButton("查重");//按照ISBN查询的按钮
JButton selectB2 = new JButton("返回主页");//按照书名查询的按钮

JTextArea insideISBN = new JTextArea();
JScrollPane isbn = new JScrollPane(insideISBN); String mutiple = "";

JScrollPane scrollPane;
    ImageIcon icon;
    Image image;
 
    public Batch(String title)
    {
     super(title);
    
        icon = new ImageIcon("img/bg.jpg");
 
        JPanel c = new JPanel()
        {
            protected void paintComponent(Graphics g)
            {
    
                g.drawImage(icon.getImage(), 0, 0, null);
                super.paintComponent(g);
            }
        };
        c.setOpaque( false );
        c.setPreferredSize( new Dimension(400, 400) );
        scrollPane = new JScrollPane( c );
        getContentPane().add( scrollPane );
  c.setLayout(null);

insideISBN.setFocusable(false); insertB.setBounds(20,30,80,30);//插入记录按钮位置,参数意义同上
selectB1.setBounds(20,90,80,30);
selectB2.setBounds(300,320,120,30); isbn.setBounds(200,30,220,250);//isbn导入显示框大小

c.add(selectB1);
c.add(selectB2);
c.add(insertB);
c.add(isbn);

selectB1.addActionListener(this);
selectB2.addActionListener(this);
insertB.addActionListener(this);
    }
    
    public void actionPerformed(ActionEvent e){
if(e.getSource() == insertB){//用getSource函数检查是点击了哪个按钮,这里是点击了导入按钮
try{
JFileChooser jfc = new JFileChooser();//就是弹出来选择文件对话框的控件

if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
open(jfc.getSelectedFile());//调用下面的open函数将ISBN号显示到右边的区域内
}
}
catch(Exception ex){
ex.printStackTrace();
}
}

if(e.getSource() == selectB1){//按ISBN号查询
try{
int countISBN = insideISBN.getText().split("\n").length;//得到要查询的数量
String[] text = insideISBN.getText().split("\n");//将这些ISBN放到字符串数组里
String[] exist = new String[countISBN];//记录查询的书是否已经存在的数组 String showResult = "";
String oneRecord = "";//每个ISBN号和它是否存在的标识的组合

for (int i=0 ; i < countISBN ; i++){
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("jdbc:odbc:book");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from book where ISBN = " + "'" + text[i].trim() +"'");

if( rs.next() ){
String isbnStr = rs.getString(1);
String nameStr = rs.getString(2);
String authorStr = rs.getString(3);
int countStr = rs.getInt(4);

exist[i] = "(ISBN)----" + nameStr + "(书名)----" + authorStr + "(作者)----" + countStr + "(数量)" ;//如果该书已经存在,那么在记录结果数组里表示出来
}

else{
exist[i] = "不存在该图书的相关信息";//如果该书不存在,那么不显示提示
}
oneRecord = text[i] + "\t\t" + exist[i] + "\n";//每行的记录,对应每个ISBN
showResult += oneRecord;//将每行的记录合成字符串
}
JOptionPane.showMessageDialog(null, showResult );
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, "ISBN号不能带特殊字符('),请检查文件!");
}
}

if(e.getSource() == selectB2){//按书名查询
this.setVisible(false);

System frame = new System("图书查重系统");
  frame.setBounds(300,150,500,450);//在Windows界面上的位置
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setResizable(false);
  frame.setVisible(true);//必须加上,默认为隐藏
}
}

private void open(File file){
   try{
         //Read from the specified file and store it in insideISBN         
       BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
       byte[] b = new byte[in.available()];
       in.read(b, 0, b.length);
       insideISBN.setText("");
       insideISBN.append(new String(b,0,b.length));
       in.close();
       }
       catch (IOException ex){
        ex.printStackTrace();
       }
   } public void keyTyped(KeyEvent e){
}
public void keyReleased(KeyEvent e){
}
public void keyPressed(KeyEvent e){
char v = e.getKeyChar();
if(v == KeyEvent.VK_ENTER){
}
}
 
    public static void main(String [] args)
    {
  Batch frame = new Batch("图书查重系统");
  frame.setBounds(260,150,600,400);//在Windows界面上的位置
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setResizable(false);
  frame.setVisible(true);//必须加上,默认为隐藏
    }
}

解决方案 »

  1.   

     StringBuffer sbb = new StringBuffer();
    //把要生成的内容放到tringBuffer里面
    try{                   BufferedReader brr = new BufferedReader(new StringReader(sbb.toString()));
                    
                      String fileName2 = "文件名.txt";
                      PrintWriter outt = new PrintWriter(new BufferedWriter(new FileWriter(路径)));
                      while((ss=brr.readLine())!=null){
                          outt.println(ss);
                      }
                      outt.close();
                      brr.close();
                  }catch(Exception e){
                      e.printStackTrace();
                  }
      

  2.   


    PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("D:\\DBdata.txt")));
            ResultSet rs = null;//这里是你的检索的结果集!
            while (rs.next()) {//这里循环
                pw.write("名字:"+rs.getString(1));
                pw.write("性别:"+rs.getString(2));
                pw.write("年龄:"+rs.getString(3));
                pw.write("成绩:"+rs.getString(4));
                pw.println();
            }
            pw.close();
      

  3.   

    将返回的记录集写入文件的方法,但愿有你能参考的地方FileWriter fw = new FileWriter("a.out");
    ......
    ResultSet rs = stmt.executeQuery(sql);
    if (rs != null && !rs.isAfterLast()) {
    while (rs.next()) {
    int cc = rs.getMetaData().getColumnCount();
    for (int i = 1; i <= cc; i++) {
    fw.write(rs.getString(i) + "\t\t");
    }
    fw.write("\n");
    }
    }
    ......
      

  4.   

    FileWriter fw=new FileWriter(new File("d:/11.txt"));BufferedWriter bw=new BufferedWriter(fw);String string=jTextField.getText();bw.write(string);