Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从较长的文件路径获取文件路径的一部分_Java_String_File - Fatal编程技术网

Java 从较长的文件路径获取文件路径的一部分

Java 从较长的文件路径获取文件路径的一部分,java,string,file,Java,String,File,在我的工具中,我让用户选择一个特定的文件。通过对该文件调用getAbsolutePath(),我将得到一个字符串,如 C:\folder\folder\folder\dataset\MainFolder\folder\folder\folder\myfile.xml 如何输入存储在新字符串变量中的“MainFolder”的路径。 我想从上面的例子中得到的是 C:\folder\folder\folder\dataset\MainFolder\ 结构始终是 驱动器:\random\number\o

在我的工具中,我让用户选择一个特定的文件。通过对该文件调用
getAbsolutePath()
,我将得到一个字符串,如

C:\folder\folder\folder\dataset\MainFolder\folder\folder\folder\myfile.xml

如何输入存储在新字符串变量中的“MainFolder”的路径。 我想从上面的例子中得到的是

C:\folder\folder\folder\dataset\MainFolder\

结构始终是

驱动器:\random\number\of\folders\dataset\main\u folder\u name\folder1\folder2\folder3\myfile.xml


我要查找的父文件夹的名称始终为“dataset”。该文件夹后面的是我感兴趣的文件夹。

我强烈建议使用文件API,而不是字符串操作,这将您与前向与反斜杠或任何其他差异的平台差异隔离开来

  • 继续“向上一个”,直到到达根目录,其中getParentFile()返回null
  • 如果你发现文件夹在你需要的过程中打破循环
  • 跟踪最后一个父级,以便在找到“数据集”后可以引用“主文件夹名称”
代码

输出

C:\random\number\of\folders\dataset\main_folder_name

在程序中使用以下代码。这将解决您的期望:)


URI
类有一个relatiize和一个toURI文件

    String path = "C:\\folder\\folder\\folder\\dataset\\MainFolder\\folder\\folder\\folder\\myfile.xml";
    String base = "C:\\folder\\folder\\folder\\dataset\\MainFolder";
    File pathFile = new File(path);
    File baseFile = new File(base);
    URI pathURI = pathFile.toURI();
    URI baseURI = baseFile.toURI();

    URI relativeURI = baseURI.relativize(pathURI);
    System.out.println(relativeURI.toString());
    // folder/folder/folder/myfile.xml

    File relativeFile = new File(relativeURI.getPath());
    System.out.println(relativeFile.getPath());
    // folder\folder\folder\myfile.xml

这可以通过只使用String类的substring()和indexOf()方法来完成。代码是:

    String path = "C:\\random\\number\\of\\folders\\dataset\\main_folder_name\\folder1\\folder2\\folder3\\myfile.xml";
    int indexOfFirstBkSlashB4Dataset = path.indexOf("\\dataset");
    String sub1 = path.substring(0,indexOfFirstBkSlashB4Dataset);
    String sub2 = "\\dataset\\";
    String sub3Intermediate = path.substring(indexOfFirstBkSlashB4Dataset+9,path.length());
    int index2 = sub3Intermediate.indexOf("\\");
    String sub4 = sub3Intermediate.substring(0,index2+1);
    String output = sub1+sub2+sub4;
    System.out.println(output);

输出为:C:\random\number\of\folders\dataset\main\u folder\u name\

使用java.nio.file;
Path
也有
.getParent()
java.io.File有
getParent()
/
getParentFile()
提示:使用
getCanonicalPath()
而不是
getAbsolutePath()
    String path = "C:\\folder\\folder\\folder\\dataset\\MainFolder\\folder\\folder\\folder\\myfile.xml";
    String base = "C:\\folder\\folder\\folder\\dataset\\MainFolder";
    File pathFile = new File(path);
    File baseFile = new File(base);
    URI pathURI = pathFile.toURI();
    URI baseURI = baseFile.toURI();

    URI relativeURI = baseURI.relativize(pathURI);
    System.out.println(relativeURI.toString());
    // folder/folder/folder/myfile.xml

    File relativeFile = new File(relativeURI.getPath());
    System.out.println(relativeFile.getPath());
    // folder\folder\folder\myfile.xml
    String path = "C:\\random\\number\\of\\folders\\dataset\\main_folder_name\\folder1\\folder2\\folder3\\myfile.xml";
    int indexOfFirstBkSlashB4Dataset = path.indexOf("\\dataset");
    String sub1 = path.substring(0,indexOfFirstBkSlashB4Dataset);
    String sub2 = "\\dataset\\";
    String sub3Intermediate = path.substring(indexOfFirstBkSlashB4Dataset+9,path.length());
    int index2 = sub3Intermediate.indexOf("\\");
    String sub4 = sub3Intermediate.substring(0,index2+1);
    String output = sub1+sub2+sub4;
    System.out.println(output);