file.getPath() 的相对路径
relative path for file.getPath()
在这个程序中,我试图select一个文件并读取这个文件的项目的相对路径
FileChooser photo = new FileChooser();
Stage stage = new Stage();stage.setTitle("File Chooser Sample");
openButton.setOnAction((final ActionEvent t) -> {
File file = photo.showOpenDialog(stage);
if (file != null) {
System.out.println(file.getPath());;
}
});
我项目的路径是
C:\Users1\eclipse-workspace\FlexiRentGui\
我是 运行 eclipse 中的程序 ide
当我select
C:\Users1\eclipse-workspace\FlexiRentGui\res\1.jpg
而不是打印相对路径“/res/1.jpg”
它仍然打印绝对路径
C:\Users1\eclipse-workspace\FlexiRentGui\res\1.jpg
您需要获取当前 directory/project 根目录的 URI,然后使用 java.net.URI.relativize()
方法找到所选文件 w.r.t 项目根目录的相对路径。像这样:new File(cwd).toURI().relativize(file.toURI()).getPath()
.
这是伪代码:
package org.test;
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class FileChooserDemo extends Application {
public FileChooserDemo() {};
public static void main(String[] args) throws ClassNotFoundException {
FileChooserDemo.launch(FileChooserDemo.class);
}
public void chooseFileAndPrintRelativePath() {
FileChooser photo = new FileChooser();
Stage stage = new Stage();
stage.setTitle("File Chooser Sample");
Button openButton = new Button("Choose file");
openButton.setOnAction((t) -> {
File file = photo.showOpenDialog(stage);
if (file != null) {
String cwd = System. getProperty("user.dir");
System.out.println(new File(cwd).toURI().relativize(file.toURI()).getPath());
}
});
//Creating a Grid Pane
GridPane gridPane = new GridPane();
//Setting size for the pane
gridPane.setMinSize(400, 200);
gridPane.add(openButton, 0, 0);
Scene scene = new Scene(gridPane);
stage.setScene(scene);
stage.show();
}
@Override
public void start(Stage primaryStage) throws Exception {
chooseFileAndPrintRelativePath();
}
}
您可以避免使用旧的 java.io
包,而改用 java.nio
。您的代码看起来会更好一些并且会更短(也使用新库)。
为此,只需获取当前工作目录:
var pwd = Paths.get("").toAbsolutePath();
var relative = pwd.relativize(Paths.get("someOtherPath"));
希望对您有所帮助。
在这个程序中,我试图select一个文件并读取这个文件的项目的相对路径
FileChooser photo = new FileChooser();
Stage stage = new Stage();stage.setTitle("File Chooser Sample");
openButton.setOnAction((final ActionEvent t) -> {
File file = photo.showOpenDialog(stage);
if (file != null) {
System.out.println(file.getPath());;
}
});
我项目的路径是 C:\Users1\eclipse-workspace\FlexiRentGui\
我是 运行 eclipse 中的程序 ide
当我select C:\Users1\eclipse-workspace\FlexiRentGui\res\1.jpg
而不是打印相对路径“/res/1.jpg”
它仍然打印绝对路径 C:\Users1\eclipse-workspace\FlexiRentGui\res\1.jpg
您需要获取当前 directory/project 根目录的 URI,然后使用 java.net.URI.relativize()
方法找到所选文件 w.r.t 项目根目录的相对路径。像这样:new File(cwd).toURI().relativize(file.toURI()).getPath()
.
这是伪代码:
package org.test;
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class FileChooserDemo extends Application {
public FileChooserDemo() {};
public static void main(String[] args) throws ClassNotFoundException {
FileChooserDemo.launch(FileChooserDemo.class);
}
public void chooseFileAndPrintRelativePath() {
FileChooser photo = new FileChooser();
Stage stage = new Stage();
stage.setTitle("File Chooser Sample");
Button openButton = new Button("Choose file");
openButton.setOnAction((t) -> {
File file = photo.showOpenDialog(stage);
if (file != null) {
String cwd = System. getProperty("user.dir");
System.out.println(new File(cwd).toURI().relativize(file.toURI()).getPath());
}
});
//Creating a Grid Pane
GridPane gridPane = new GridPane();
//Setting size for the pane
gridPane.setMinSize(400, 200);
gridPane.add(openButton, 0, 0);
Scene scene = new Scene(gridPane);
stage.setScene(scene);
stage.show();
}
@Override
public void start(Stage primaryStage) throws Exception {
chooseFileAndPrintRelativePath();
}
}
您可以避免使用旧的 java.io
包,而改用 java.nio
。您的代码看起来会更好一些并且会更短(也使用新库)。
为此,只需获取当前工作目录:
var pwd = Paths.get("").toAbsolutePath();
var relative = pwd.relativize(Paths.get("someOtherPath"));
希望对您有所帮助。