JAVAFX文件保存代码参考

JAVAFX文件保存代码参考,第1张

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 files = fc.showOpenMultipleDialog(primaryStage);
            Iterator it = files.iterator();
            DirectoryStream ds = null;
            Path path;
            File file;
            Path _path;
            File _file;
            try {
                ds = Files.newDirectoryStream(Paths.get("d:\dir"));
                Iterator _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);

    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5661253.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存