JAVAFX文件保存代码参考

package com.task;

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class FileCopyFx extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField tf = new TextField();
        tf.setPrefWidth(500);
        Button btn = new Button("复制文件");
        HBox hb = new HBox();
        hb.getChildren().addAll(tf, btn);
        Scene scene = new Scene(hb, 1000, 500);
        primaryStage.setScene(scene);
        primaryStage.show();
        btn.setOnAction((e) -> {
            FileChooser fc = new FileChooser();
            List<File> files = fc.showOpenMultipleDialog(primaryStage);
            Iterator<File> it = files.iterator();
            DirectoryStream<Path> ds = null;
            Path path;
            File file;
            Path _path;
            File _file;
            try {
                ds = Files.newDirectoryStream(Paths.get("d:\\dir"));
                Iterator<Path> _it = ds.iterator();
                while (it.hasNext() && _it.hasNext()) {
                    file = it.next();
                    path = file.toPath();
                    _path = _it.next();
                    _file = _path.toFile();
                    System.out.println(file.getPath());
                    System.out.println(_file.getPath());
                    Files.copy(path, _path, REPLACE_EXISTING);
                    file = new File(_path.getParent().toFile().getPath(), file.getName());
                    _file.renameTo(file);
                    Files.move(file.toPath(), Paths.get(tf.getText() + File.separator + file.getName()), REPLACE_EXISTING);

                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        });

    }

    public static void main(String[] args) throws IOException {
        launch(args);

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

car-java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值