相对路径获取文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/winteriscomming/article/details/82383779
//获取文件File file = new File(url);

//这个获取的是未编译的本地文件
File file = new File("src/main/resources/templates/changeActionRichText/freemarker/ChangeActionRichText.html");
//由于打包后不存在什么src地址,所以不要代码中用上面的方式获取。用下面的方式获取编译后的相对地址
//前者用getResource获取到地址,后者用getClassLoader().getResourceAsStream
URL url1 = this.getClass().getResource("/");    //这里获取的是bin目录,也就是编译后的根目录
InputStream inputStreamRoot = this.getClass().getClassLoader().getResourceAsStream("/");//这里获取的是bin目录,也就是编译后的根目录
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(						"templates/changeActionRichText/freemarker/ChangeActionRichText.html");
URL url2 = this.getClass().getResource("/templates/changeActionRichText/freemarker/ChangeActionRichText.html");        //假设想获取这个文件的地址
//获取文件中的内容
String info = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining(System.lineSeparator()));
//获取文件中的内容
String info = FileUtils.readFileToString(file);

猜你喜欢

转载自blog.csdn.net/winteriscomming/article/details/82383779