Java FileSystem getSeparator () ,springboot 项目获取项目所在目录并在同级目录下创建文件夹存放文件
-
String sepa = java.io.File.separator;
String sepa1 = System.getProperty("file.separator");
代码如下:System.out.println (System.getProperty ("file.separator"));
Java FileSystem getSeparator () 用法及代码示例
FileSystem 类的 getSeparator () 方法用于以字符串形式返回此文件系统的名称分隔符。名称分隔符用于分隔路径字符串中的名称。对于默认提供程序,此方法返回与 String 相同的分隔符。
用法:
public abstract String getSeparator()
参数:此方法不接受任何内容。
返回值:此方法返回与 String 相同的分隔符。
以下示例程序旨在说明 getSeparator () 方法:
示例 1:
// Java program to demonstrate
// FileSystem.getSeparator() method
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.Paths;
public class GFG {
public static void main(String[] args)
{
// create the object of Path
Path path
= Paths.get(
"C:\\Movies\\document.txt");
// get FileSystem object
FileSystem fs = path.getFileSystem();
// apply getSeparator() methods
String separator = fs.getSeparator();
// print
System.out.println("Seperator: "
+ separator);
}
}
输出:
示例 2:
// Java program to demonstrate
// FileSystem.getSeparator() method
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.Paths;
public class GFG {
public static void main(String[] args)
{
// create the object of Path
Path path
= Paths.get(
"E:// Tutorials// file.txt");
// get FileSystem object
FileSystem fs = path.getFileSystem();
// apply getSeparator() methods
String separator = fs.getSeparator();
// print
System.out.println("File Seperator: "
+ separator);
}
}
输出:
![](https://img2024.cnblogs.com/blog/572188/202406/572188-20240625153240551-1930021341.jpg)
参考:https://docs.oracle.com/javase/10/docs/api/java/nio/file/FileSystem.html#getSeparator ()
String path = new File(ResourceUtils.getURL("classpath:").getPath()).getParentFile().getParentFile().getParent();
我的项目名叫 LQTProject 是用 idea 构建的。
放在 C:\Users\hp\IdeaProjects 下
当使用 idea 发布 springboot 项目直接运行,而不是打包为 jar 运行的时候,运行上面的代码获取的是项目的位置:
运行上面代码获得路径:C:\Users\hp\IdeaProjects
完整代码
-
public static String getWebFile(){
-
String jar_parent="";
-
try {
-
jar_parent = new File(ResourceUtils.getURL("classpath:").getPath()).getParentFile().getParentFile().getParent();
-
jar_parent+= File.separator+"LQTProject_file"+File.separator;
-
-
} catch (FileNotFoundException e) {
-
e.printStackTrace();
-
}
-
return jar_parent;
-
摘抄自网络,便于检索查找。