gpt4 book ai didi

java - Java 中透明且与操作系统无关的路径处理

转载 作者:行者123 更新时间:2023-11-30 06:30:07 25 4
gpt4 key购买 nike

我正在为我们的应用程序开发图形安装程序。由于没有可用的安装程序生成器满足要求和限制,因此我从头开始构建它。

安装程序应该在多个操作系统上运行,因此路径处理需要与操作系统无关。为此,我编写了以下小实用程序:

public class Path {
private Path() {
}

public static String join(String... pathElements) {
return ListEnhancer.wrap(Arrays.asList(pathElements)).
mkString(File.separator);
}

public static String concatOsSpecific(String path, String element) {
return path + File.separator + element;
}

public static String concatOsAgnostic(String path, String element) {
return path + "/" + element;
}

public static String makeOsAgnostic(String path) {
return path.replace(File.separator, "/");
}

public static String makeOsSpecific(String path) {
return new File(path).getAbsolutePath();
}

public static String fileName(String path) {
return new File(path).getName();
}
}

现在我的代码在许多地方充斥着 Path.*AgnosticPath.*Specific 调用。很明显,这很容易出错,而且根本不透明。

我应该采取什么方法来使路径处理透明且不易出错?是否存在任何已经解决此问题的实用程序/库?任何帮助将不胜感激。

编辑:

为了举例说明我的意思,这是我刚才写的一些代码。 (题外话:请原谅冗长的方法。代码处于初始阶段,很快将进行一些重大重构。)

一些上下文:ApplicationContext 是一个存储安装数据的对象。这包括多个路径,例如 installationRootDirectoryinstallationDirectory 等。这些路径的默认值是在创建安装程序时指定的,因此始终以与操作系统无关的格式存储。

@Override
protected void initializeComponents() {
super.initializeComponents();
choosePathLabel = new JLabel("Please select the installation path:");
final ApplicationContext c = installer.getAppContext();
pathTextField = new JTextField(
Path.makeOsSpecific(c.getInstallationDirectory()));
browseButton = new JButton("Browse",
new ImageIcon("resources/images/browse.png"));
browseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setAcceptAllFileFilterUsed(false);
int choice = fileChooser.showOpenDialog(installer);
String selectedInstallationRootDir = fileChooser.getSelectedFile().
getPath();
if (choice == JFileChooser.APPROVE_OPTION) {
c.setInstallationRootDirectory(
Path.makeOsAgnostic(selectedInstallationRootDir));
pathTextField.setText(Path.makeOsSpecific(c.getInstallationDirectory()));
}
}
});
}

最佳答案

或者您可以引入 2 个新类:

class OsSpecificPath implements FilePathInterface
{
String path;

OsAgnosticPath toAgnosticPath();

OsSpecificPath concat( OsSpecificPath otherPath );

// from IFilePath
getFile();

... etc
}

class OsAgnosticPath implements FilePathInterface
{
String path;

OsSpecificPath toOsSpecificPath();

OsAgnosticPath concat( OsAgnosticPath otherPath );

// from IFilePath
getFile();

... etc
}

每个人都根据需要包装一条路径。

每个方法都可以有方法转换为其他类型的路径,但不是“字符串类型”的解决方案,其中一切都是字符串并且可能被滥用,你有 2 个强类型的类,它们不能被错误地传递。

任何不关心路径类型的东西都会使用 FilePathInterface,任何需要对特定类型的路径进行操作的东西都会使用这些类型。如果确实有必要,FilePathInterface 可以假设在界面中同时包含 toAgnosticPathtoOsSpecificPath...

关于java - Java 中透明且与操作系统无关的路径处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10840512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com