保存 JTextPane,然后打开它 Java



我正在尝试使用JFileChooser保存文件,但我似乎无法让它真正保存。我已经尝试了几种不同的方法,但我希望用户能够保存在所需的位置。当我点击"保存"按钮时,它说它已保存。但是当我尝试查找该文件时,它不存在。

public class GUI extends JFrame implements ActionListener, DocumentListener{
JMenuItem aapne;
JMenuItem lagre;
JMenuItem sok;
JMenuItem farge;
JMenuItem oversett;
JTextPane tekstomraadet;
JTextPane dir;
String fratekst;
String tiltekst;
String tekst;

DefaultListModel listmodel = new DefaultListModel();
JList liste = new JList(listmodel);
public GUI(){
    this.setSize(1000,500);
    this.setLayout(null);
    //Passord
    passord pass = new passord();
    pass.password = JOptionPane.showInputDialog(null, "Skriv inn et passord");
    while(!(pass.password.equals("words"))){
        pass.password = JOptionPane.showInputDialog(null, "Skriv inn passordet på nytt");
    }
    //Liste

    liste.setBounds(900,0,100,600);
    liste.setBackground(Color.pink);
    this.add(liste);
    liste.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    listmodel.addElement("Hvit");
    listmodel.addElement("Svart");

    //Tekst

    tekstomraadet = new JTextPane();
    dir = new JTextPane();
    tekstomraadet.setBounds(0, 0, 890, 600);
    tekstomraadet.setContentType("text/html");
    tekstomraadet.getDocument().addDocumentListener(this);
    this.add(tekstomraadet);
    //MENY
    JMenuBar menuBar = new JMenuBar();
    JMenu fil = new JMenu("Fil");
    aapne = new JMenuItem("Åpne");
    lagre = new JMenuItem("Lagre");
    sok = new JMenuItem("Søk");
    farge = new JMenuItem("Endre farge");
    oversett = new JMenuItem("Oversett");
    aapne.addActionListener(this);
    lagre.addActionListener(this);
    sok.addActionListener(this);
    farge.addActionListener(this);
    oversett.addActionListener(this);
    fil.add(aapne);
    fil.add(lagre);
    fil.add(sok);
    fil.add(farge);
    fil.add(oversett);
    menuBar.add(fil);
    this.setJMenuBar(menuBar);
    this.setVisible(true);
}
//OVERSETTING
public String oversett (String input){
    input = input.replace(" en ", " 1 ");
    input = input.replace(" to ", " 2 ");
    input = input.replace(" tre ", " 3 ");
    input = input.replace(" fire ", " 4 ");
    input = input.replace(" fem ", " 5 ");
    input = input.replace(" seks", " 6 ");
    input = input.replace(" syv ", " 7 ");
    input = input.replace(" åtte ", " 8 ");
    input = input.replace(" ni ", " 9 ");
    input = input.replace(" ti ", " 10 ");
    return input;

}
public String Ord (String Ordene, String teksten){
    teksten = teksten.replace(Ordene, "<font color="red">"+Ordene+"</font>");
    return teksten;
}
public String Ord2 (String Ordene, String teksten){
    teksten = teksten.replace(Ordene, "<font color="WHITE">"+Ordene+"</font>");
    return teksten;
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if(e.getSource().equals(farge)){
        if(liste.getSelectedValue().equals("Hvit")){
            tekstomraadet.setBackground(Color.WHITE);
        }
        else if(liste.getSelectedValue().equals("Svart")){
            tekstomraadet.setBackground(Color.BLACK);
            String ord = tekstomraadet.getText();
            String gammeltekst = tekstomraadet.getText();
            String nytekst = Ord2(ord, gammeltekst);
            tekstomraadet.setText(nytekst);
        }
    }
    else if (e.getSource().equals(lagre)){
        JFileChooser filechooser = new JFileChooser();
        filechooser.setDialogTitle("Spesifiser en fil for lagring");
        int rVal = filechooser.showSaveDialog(GUI.this);
        if(rVal == JFileChooser.APPROVE_OPTION){
            File fileTosave = filechooser.getSelectedFile();
            System.out.println("Save as file: " +fileTosave.getAbsolutePath());
            try {
                FileOutputStream f = new FileOutputStream("hehe");
                ObjectOutputStream o = new ObjectOutputStream(f);
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
    else if (e.getSource().equals(aapne)){
        JFileChooser c = new JFileChooser();
        int rVal = c.showOpenDialog(GUI.this);
        if (rVal == JFileChooser.APPROVE_OPTION){
            tekstomraadet.setText(c.getSelectedFile().getName());
        }
    }
    else if (e.getSource().equals(oversett)){
        fratekst = tekstomraadet.getText();
        tiltekst = this.oversett(fratekst);
        tekstomraadet.setText(tiltekst);

    }
    else if( e.getSource().equals(sok)){
        String ord = JOptionPane.showInputDialog(this, "Hvilket ord søker du etter?");
        String gammeltekst = tekstomraadet.getText();
        String nytekst = Ord(ord, gammeltekst);
        tekstomraadet.setText(nytekst);

    }
}


@Override
public void changedUpdate(DocumentEvent arg0) {
    // TODO Auto-generated method stub
}
@Override
public void insertUpdate(DocumentEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void removeUpdate(DocumentEvent arg0) {
    // TODO Auto-generated method stub
}
}

如果我使用固定保存,它可以工作。但这只是打开一个文件的一项艰巨工作。

FileOutputStream f = new FileOutputStream("hehe");
ObjectOutputStream o = new ObjectOutputStream(f);

您从不使用任何输出流进行写入。您需要写入输出流,然后刷新,然后关闭它。我相信关闭它(应该在 finally 块中完成)通常会刷新输出流。

FileOutputStream f = new FileOutputStream("hehe");
ObjectOutputStream o = null;
try {
   o = new ObjectOutputStream(f);
   while (objectsAreLeftToBeWritten) {
     // get next object and write with 
     // o.writeObject(nextObject);
   }
} catch(....) {
  // .... etc
} finally {
  // if o is not null, close it
}

相关内容

最新更新