为什么这个程序返回IOException?

时间:2014-04-27 17:39:20

标签: java

请我在这个程序中遇到问题它返回给我一个IOExeption我正在使用net-beans我测试了很多次,我发现问题出在 fichier=new FileWriter("../test.txt",true);

这是代码源: 我想它不会出现在IDE中,但如果我们使用例如Excesior JET http://www.excelsiorjet.com/进行.exe,你会发现存在异常IO。我尝试纠正它,但我没有发现问题确切的FileWriter语法是正确的,我在最后关闭文件

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package te;

import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Khalid
 */
public class Fenetre extends javax.swing.JFrame {

    /**
     * Creates new form Fenetre
     */
    FileWriter fichier;
    public Fenetre() {
        initComponents();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("implanter");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(158, 158, 158)
                .addComponent(jButton1)
                .addContainerGap(165, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(248, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

        try {
            Class.forName("com.mysql.jdbc.Driver");
             String url = "jdbc:mysql://localhost:3306";
             Connection conn = (Connection)DriverManager.getConnection(url,"root","");
             Statement state=conn.createStatement();
             ResultSet r=state.executeQuery("select NOW()");
         fichier= new FileWriter("../test.txt",true);
         JOptionPane.showMessageDialog(null,"test 1 après new filewriter");
            if(r.next()){
         fichier.write(jButton1.getActionCommand()+": "+" test "+" Date: "+r.getString(1)+"\r\n");
         JOptionPane.showMessageDialog(null,"test 1 après write");
            fichier.flush();
            JOptionPane.showMessageDialog(null,"test 1 après flush");

            }
            fichier.close();
            JOptionPane.showMessageDialog(null,"test 1 après close");
            conn.close();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null,"erreur IOEXCEPTION");
        }


        catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"erreur SQLEXCE");
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null,"erreur classnotfound");
        } 
    }

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration
}

1 个答案:

答案 0 :(得分:0)

如果有人跟我一样,我发现了问题。 检查创建的文件,它是在与Windows 7一样的操作系统文件夹所在的同一文件夹中创建的,所以,在这部分我们不能添加一些东西;打开文件进行写入,创建文件,...(拒绝访问)这些操作我们必须具有root权限