时间:2024-10-03 20:01:32
如何读取resources目录下的文件路径(九种方式)
直接通过文件名+getFile()来获取文件。如果是文件路径的话getFile和getPath效果是一样的,如果是URL路径的话getPath是带有参数的路径。如下所示:
使用getFile()方式获取文件的代码如下:
/** * 直接通过文件名+getFile()来获取 * * @param fileName * @throws IOException */ public void function3 ( String fileName ) throws IOException { String path = this . getClass ( ) . getClassLoader ( ) . getResource ( fileName ) . getFile ( ) ; //注意getResource("")里面是空字符串 System . out . println ( path ) ; String filePath = URLDecoder . decode ( path , "UTF-8" ) ; //如果路径中带有中文会被URLEncoder,因此这里需要解码 System . out . println ( filePath ) ; getFileContent ( filePath ) ;
《读取resources目录下的文件》不代表本网站观点,如有侵权请联系我们删除