Zapisywanie pliku

0

Hej! Pisze prosty edytor tekstu, jednak napotkałem problem przy zapisywaniu pliku, otwieranie działa bez problemu. Przy kompilacji wyskakuje błąd ze cos nie tak przy

 while ( (s = br.getText(String)) != null ) {
        jtaEdycja.append(s));
        jtaEdycja.append("\n");

moglibyście rzucic na to okiem?

Otwieranie:

private void OtworzActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        javax.swing.JFileChooser jfc = new javax.swing.JFileChooser();
int wynik = jfc.showOpenDialog(this);
if (wynik != javax.swing.JFileChooser.APPROVE_OPTION) {
    return;
}
java.io.File plik = jfc.getSelectedFile();
try {
    java.io.BufferedReader br = new java.io.BufferedReader(
                                new java.io.FileReader(plik)
                                );
    String s;
    jtaEdycja.setText("");
    while ( (s = br.readLine()) != null ) {
        jtaEdycja.append(s);
        jtaEdycja.append("\n");
    }            
    br.close();
} catch (Exception e) {
    javax.swing.JOptionPane.showMessageDialog(this,"Błąd wejścia/wyjścia");
    return;
}

Zapisywanie:

 private void ZapiszActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        javax.swing.JFileChooser jfc = new javax.swing.JFileChooser();
int wynik = jfc.showSaveDialog(this);
if (wynik != javax.swing.JFileChooser.APPROVE_OPTION) {
    return;
}
java.io.File plik = jfc.getSelectedFile();
try {
    java.io.BufferedWriter br = new java.io.BufferedWriter(
                                new java.io.FileWriter(plik)
                                );
    String s;
    jtaEdycja.setText("");
    while ( (s = br.getText(String)) != null ) {
        jtaEdycja.append(s));
        jtaEdycja.append("\n");
    }            
    br.close();
} catch (Exception e) {
    javax.swing.JOptionPane.showMessageDialog(this,"Błąd wejścia/wyjścia");
    return;
}
    }

Będę wdzięczny za każdą radę, wskazówkę, pozdrwiam!

0

o ile się nie mylę to <java>(s = br.getText(String))</java> porównuje ci dwa string'i a potem wynik który będzie logiczny ( True / False ) porównujesz z >b>null</b>'em

nie znam zbytnio Javy ale raczej powinieneś sprawdzić jaki wynik logiczny zwraca to porównanie ciągów, jeżeli ma zapętlać dopóki wychodzi True to nie musisz raczej robić sprawdzenia bo while wykonuje pętle bodajże wtedy gdy całe wyrażenie warunkowe da wynik True

0

A może tak?

BufferedWriter br = new java.io.BufferedWriter(new FileWriter(plik));
br.write(jtaEdycja.getText());
br.close();
0

śmierc_na_drodze dzięki CI bardzo za poprawke!!! Wszystko gra! Chciałbym dodac jeszcze do tego edytora obsługę czcionek, możliwośc zmiany kroju i wielkości, napisałem cos takiego:

/*
 * FCzcionka.java
 *
 * Created on 2 czerwiec 2008, 18:05
 */

package edytor;

import java.awt.Font;

/**
 *
 * @author  Michał
 */
public class FCzcionka extends javax.swing.JDialog {
    private FEdytor edytor;
    private Font[] czcionki;
    
    /** Creates new form FCzcionka */
    public FCzcionka(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    java.awt.GraphicsEnvironment e =
        java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
    czcionki = e.getAllFonts();
    java.util.Vector v = new java.util.Vector();
    for (int i = 0; i < czcionki.length; i++)
    v.add(czcionki[i].getName());
    jList1.setListData(v);
}
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jSpinner1 = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(null);

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(10, 30, 200, 150);

        jLabel1.setText("Krój czcionki:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 10, 150, 14);

        jSpinner1.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                jSpinner1AncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        getContentPane().add(jSpinner1);
        jSpinner1.setBounds(300, 90, 40, 20);

        jLabel2.setText("Wielkosc czcionki");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(280, 60, 90, 14);

        jButton1.setText("OK");
        getContentPane().add(jButton1);
        jButton1.setBounds(225, 200, 70, 23);

        jButton2.setText("Anuluj");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(310, 200, 65, 23);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-400)/2, (screenSize.height-271)/2, 400, 271);
    }// </editor-fold>                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        dispose();
    }                                        

    private void jSpinner1AncestorAdded(javax.swing.event.AncestorEvent evt) {                                        
int c = jList1.getSelectedIndex();
if (c == -1) return; // nic nie wybrano
// nasz spinner operuje na liczbach, jego model to SpinnerNumberModel
javax.swing.SpinnerNumberModel m = 
    (javax.swing.SpinnerNumberModel) jSpinner1.getModel();
int rozm = m.getNumber().intValue();
// wyprowadzamy nową czcionkę ze stylem 0 i zadanym rozmiarem
java.awt.Font f = czcionki[c].deriveFont(0,rozm);
edytor.jtaEdycja.setFont(f); // ustawiamy nową czcionkę w oknie edycji
setVisible(false);
dispose();
edytor.jtaEdycja.requestFocus();        // TODO add your handling code here:
    }                                       
    
    /**
     * @param args the command line arguments
     */
   
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner jSpinner1;
    // End of variables declaration                   
    
}

Jak kompiluje w netBeansie niby wszytsko jest ok, ale w JCreatorze wystepują błędy:

at java.awt.Component.dispatchEvent(Component.java:3819)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Process completed.

P.S. Dlaczego w NetBeansie po skompilowaniu i uruchomieniu projektu wyskakuje w okienku Output BUILD SUCCESSFUL a nie widac efektu mojej pracy?

0

Napisałem do Ciebie na mailu jeśli możesz zobacz to jak najszybciej i odpisz.
pozdrawiam

1 użytkowników online, w tym zalogowanych: 0, gości: 1