JAVAFX文件保存代码参考

2021/12/12 1:19:46

本文主要是介绍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);

    }
}



这篇关于JAVAFX文件保存代码参考的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程