Topic: FileDialog如何实现存盘和打开文件的功能?

  Print this page

1.FileDialog如何实现存盘和打开文件的功能? Copy to clipboard
Posted by: OwenZhu
Posted on: 2004-10-13 23:20

FileDialog 中怎样实验存盘的功能?
在打开文件时,我的在只能把文件的标题显示在TextArea内?请问高手阁下可否为鄙人指点?感激不尽!

==================
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class NotePad extends JFrame
{
  private NotePad form;
  JMenuBar menubar=new JMenuBar();
  JMenu file=new JMenu("File");
  
  JMenuItem open=new JMenuItem("Open");
  JMenuItem saveas=new JMenuItem("Save As");
  
  JTextArea text=new JTextArea();
  JScrollPane scollpane=new JScrollPane(text);
  
  public NotePad()
  {
    Container contentpane=this.getContentPane();
    setJMenuBar(menubar);
    
    open.addActionListener(new fileActionListener());
    saveas.addActionListener(new fileActionListener());
    file.add(open);
    file.add(save);
    menubar.add(file);
    
    contentpane.add(scrollpane);
    this.setTitle("New file");
    this.setSize(500,400);
    this.setVisible(true);
     form=this;
   }
  
  class fileActionListener implements ActionListener
   {
     public void actionPerformed(ActionEvent e)
     {        
      if(e.getSource()==open)
      {
        //public static final int LOAD=1;
      
        FileDialog openDialog=new FileDialog(form,"Load",FileDialog.LOAD);
        openDialog.show();
        if(openDialog.getFile() != null)
        text.setText(openDialog.getFile());
      }
      if(e.getSource()==saveas)
      {
        FileDialog openDialog=new FileDialog(form,"Save As",FileDialog.SAVE);
        openDialog.setVisible(true);
      }
    }
   }  
  
   public void static main(String[] args)
   {
     new NotePad();
   }
}

2.Re:FileDialog如何实现存盘和打开文件的功能? [Re: OwenZhu] Copy to clipboard
Posted by: kavinwang
Posted on: 2004-10-14 09:39

存盘和你的数据怎样存,需要你自己去实现。
一般是通过对话框选择需要保存得文件和目录,完成后调用自己得存盘代码进行存盘。

3.Re:FileDialog如何实现存盘和打开文件的功能? [Re: OwenZhu] Copy to clipboard
Posted by: ecsoftcn
Posted on: 2004-12-15 17:03

那要看你的存取目标了,我用的文本文件,可以用PrintWriter实现
用System.out()写入

4.Re:FileDialog如何实现存盘和打开文件的功能? [Re: OwenZhu] Copy to clipboard
Posted by: quxiaofei
Posted on: 2004-12-19 16:12

看可以不?
private void doSave(String fileName){
      FileOutputStream fos = null;
      if(fileName==""){
        doSaveAs();
        }else {
          String str = textBox.getText();
          try{
            fos = new FileOutputStream(fileName+".txt");
            fos.write(str.getBytes());
            }catch(IOException e){}
          
        finally{
          try{
            fos.close();
            }catch(IOException e1){}
          }
          changed=false;}
          }
    private void doSaveAs(){
      FileOutputStream fos = null;
      String str = textBox.getText();
      FileDialog fileDialog = new FileDialog(this,"另存为...",FileDialog.SAVE);
      fileDialog.show();
      if(fileDialog.getFile()==null){
      return;
     }
     fileName = fileDialog.getDirectory()+File.separator+fileDialog.getFile();
     try{
       fos = new FileOutputStream(fileName+".txt");
       fos.write(str.getBytes());
       }catch(IOException e){}
       finally{
         try{fos.close();}
         catch(IOException e1){}
       }
       changed = false;
      }


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923