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();
    }

}

你可能感兴趣的:(代码笔记)