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文件保存代码参考
最新推荐文章于 2023-02-10 16:58:52 发布