如何从给定的文件路径获取子路径

  • 本文关键字:路径 文件 获取 java
  • 更新时间 :
  • 英文 :


我想在给定标记后获取路径"这是一个固定标记,文件路径在

下面
String token = "html"
Path path = D:datatesthtmlcssCore.css
Expected Output : cssCore.css

下面的是程序的输入文件夹。并在代码中定义为常量。

public static final String INPUT_DIR = "D:datatesthtml" 

将包含输入html, css, js文件。并希望将这些文件复制到不同的位置E:datatesthtml这里,所以只需要从输入文件路径中提取html后的子路径,将其附加到输出路径。

假设输入文件是

D:datatesthtmlcssCore.css
D:datatesthtmlcssCore.html
D:datatesthtmlcssCore.js

所以要提取cssCore.css, cssCore.html, cssCore.js附加到目标路径E:datatesthtml复制它。

下面的尝试String [] array = path.tostring().split("html");String subpath = array[1];

Output : cssCore.css 

,这不是预期的输出预期的输出是cssCore.css上面的代码也不能在

路径下工作。
Path path = D:datatesthtmlblablahtmlcssCore.css;
String [] array = path.toString().split("html");
String subpath = array[1];

在这种情况下,我得到类似blabla的东西,这不是预期。

如果您只需要字符串形式的路径,另一个解决方案是使用以下代码:

String path = "D:\data\test\html\css\Core.css";
String keyword = "\html";
System.out.println(path.substring(path.lastIndexOf(keyword) + keyword.length()).trim());

您可以像上面提到的那样用file.getAbsolutePath()替换路径。


import java.io.File;

public class Main {
public static void main(String[] args) {
// Create a File object for the directory that you want to start from
File directory = new File("/path/to/starting/directory");

// Get a list of all files and directories in the directory
File[] files = directory.listFiles();

// Iterate through the list of files and directories
for (File file : files) {
// Check if the file is a directory
if (file.isDirectory()) {
// If it's a directory, recursively search for the file
findFile(file, "target-file.txt");
} else {
// If it's a file, check if it's the target file
if (file.getName().equals("target-file.txt")) {
// If it's the target file, print the file path
System.out.println(file.getAbsolutePath());
}
}
}
}

public static void findFile(File directory, String targetFileName) {
// Get a list of all files and directories in the directory
File[] files = directory.listFiles();

// Iterate through the list of files and directories
for (File file : files) {
// Check if the file is a directory
if (file.isDirectory()) {
// If it's a directory, recursively search for the file
findFile(file, targetFileName);
} else {
// If it's a file, check if it's the target file
if (file.getName().equals(targetFileName)) {
// If it's the target file, print the file path
System.out.println(file.getAbsolutePath());
}
}
}
}
}

此代码使用递归函数搜索起始目录的所有子目录,并在找到目标文件时打印目标文件的文件路径(在本例中为"target-file.txt")。

您可以修改此代码以满足您的特定需求,例如更改起始目录或目标文件名。您还可以修改代码以对目标文件执行不同的操作,例如读取其内容或将其复制到另一个位置。

你的问题缺乏细节。

  • 是路径;路径还是字符串?
  • 如何确定"路径"的哪一部分?你想要什么?
  • 你知道整个"路径"的结构吗?还是只有分隔符部分,例如html?

这里有六种不同的方法(没有迭代,正如你在评论中所说的)。前两种使用java.nio.file.Path的方法。下面两种使用java.lang.String的方法。最后两个使用正则表达式。请注意,可能还有其他方法。

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PathTest {
public static void main(String[] args) {
// D:datatesthtmlcssCore.css
Path path = Paths.get("D:", "data", "test", "html", "css", "Core.css");
System.out.println("Path: " + path);
Path afterHtml = Paths.get("D:", "data", "test", "html").relativize(path);
System.out.println("After 'html': " + afterHtml);
System.out.println("subpath(3): " + path.subpath(3, path.getNameCount()));
String str = path.toString();
System.out.println("replace: " + str.replace("D:\data\test\html\", ""));
System.out.println("substring: " + str.substring(str.indexOf("html") + 5));
System.out.println("split: " + str.split("\\html\\")[1]);
Pattern pattern = Pattern.compile("\\html\\(.*$)");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
System.out.println("regex: " + matcher.group(1));
}
}
}

运行以上代码产生以下输出:

Path: D:datatesthtmlcssCore.css
After 'html': cssCore.css
subpath(3): cssCore.css
replace: cssCore.css
substring: cssCore.css
split: cssCore.css
regex: cssCore.css

我想你知道如何修改上面的内容,以便

我想获得/test后的文件路径