private static String FILE_NAME = "zxl.txt";
第一种 new ClassPathResource(文件名)
ClassPathResource classPathResource = new ClassPathResource(FILE_NAME);
File file = classPathResource.getFile();
String filePath = file.getPath();
第二种 String的工具类 ResourceUtils.getFile("classpath:" + FILE_NAME);
File file = ResourceUtils.getFile("classpath:" + FILE_NAME);
String filePath = file.getPath();
第三种 this.getClass().getResource("/") + FILE_NAME;
String s = this.getClass().getResource("/") + FILE_NAME;
String filePath = s.substring(6);
注:
jar里面文件读取方式:(jar包是封闭的东西无法通过路径去获取需以流的方式读取)
ClassPathResource classPathResource = new ClassPathResource("test.txt");
第一种 new ClassPathResource(文件名)
ClassPathResource classPathResource = new ClassPathResource(FILE_NAME);
File file = classPathResource.getFile();
String filePath = file.getPath();
第二种 String的工具类 ResourceUtils.getFile("classpath:" + FILE_NAME);
File file = ResourceUtils.getFile("classpath:" + FILE_NAME);
String filePath = file.getPath();
第三种 this.getClass().getResource("/") + FILE_NAME;
String s = this.getClass().getResource("/") + FILE_NAME;
String filePath = s.substring(6);
注:
jar里面文件读取方式:(jar包是封闭的东西无法通过路径去获取需以流的方式读取)
ClassPathResource classPathResource = new ClassPathResource("test.txt");
获取文件流:classPathResource.getInputStream();
又或者使用:
InputStream resourceAsStream = this.getClass().getResourceAsStream("/"+FILE_NAME);