FileDialog 使用方法---JAVA

創建一個具有指定標題的文件對話框窗口,用於加載或保存文件

FileDialog(Frame parent, String title, int mode)

int mode 有SAVE(保存)和LOAD(讀寫)兩種

getDirectory()+getFile() = 文件路徑

import java.awt.FileDialog;
import java.awt.Frame;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;

public class FileOpen {

    private Frame jf;
    private PrintStream jtext;

    public static void main(String[] args) throws Exception {
        FileOpen hj = new FileOpen();
        hj.open();
        hj.save();

    }

    public void save() throws Exception{

        FileDialog fd = new FileDialog(jf, "另存爲", FileDialog.SAVE);

        fd.setVisible(true);

        FileOutputStream out = new FileOutputStream(fd.getDirectory() + fd.getFile() + ".txt" );

        Object jtext;

        String str = null;

        out.write(str.getBytes());
        out.close();
    }

    public void open() throws Exception {
        FileDialog fdopen = new FileDialog(jf, "打開", FileDialog.LOAD);

        fdopen.setVisible(true);

        BufferedReader in = new BufferedReader(new FileReader(fdopen.getDirectory() + fdopen.getFile()));

        String str = null;

        while((str = in.readLine()) != null) {
            System.out.println(str);

            jtext.append(str + "\n");
        }
        in.close();
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章