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

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值