public class TestFile
public static void main(String[] args) throws Exception
File absoluteFile = new File("c:/workspace/path_project/demo.txt");
File relativeFile = new File("demo.txt");
showFileContent(absoluteFile);
showFileContent(relativeFile);
public static void showFileContent(File file) throws Exception
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuilder contentHolder = new StringBuilder();
String lineContent = null;
while ((lineContent = br.readLine()) != null)
contentHolder.append(lineContent);
br.close();
System.out.println("content=" + contentHolder);
工程在硬盘和eclipse的目录结构如下:
在eclipse中运行上面的程序,发现2种方式都是能够正确读取文件的,不会抛FileNotFoundException。
使用绝对路径,虽然定位很清晰,但是不灵活。比如你将上面的工程放到D盘下,就必须要修改绝对路径路径,这显然很不方便。使用相对路径则跟工程所在的硬盘路径无关,直接导入eclipse中运行,就能够正确读取文件内容。而且实际情况是,很多时候我们并不知道文件的绝对路径,这会因为部署环境的不同而不同。比如将制作好的war放到tomcat或jboss容器下运行,很显然绝对路径是不同的,而我们的代码事先并不知道。
那么使用相对路径呢?很遗憾,也同样存在很多问题。File是java.io包的基础类,java.io 包中的类总是根据当前用户目录来分析相对路径名。也就是说以下2种方式是等价的,
File file1 = new File("demo.txt");
String asbPath = System.getProperty("user.dir") + "/demo.txt";
File file2 = new File(asbPath);
也就是说相对路径是否好使,取决于user.dir的值。系统属性 user.dir是JVM启动的时候设置的,通常是 Java 虚拟机的调用目录,即执行java命令所在的目录。
对于tomcat/jboss容器,user.dir是%home/bin%/目录,因为这个目录就是我们启动web容器的地方。也就是说,user.dir也是可变的,不固定的。显然使用这种方式跟绝对路径没有什么本质差别,都是不推荐的。顺便提一下,我们在eclipse中运行程序的时候,eclipse会将user.dir的值设置为工程的根目录,在我们的例子中,user.dir是c:/workspace/path_project/
可以得出结论:使用java.io.File读取文件,无论是相对路径,还是绝对路径都不是好的做法,能不使用就不要使用。
2.使用Class.getResource()或ClassLoader.getResource()
这2个方法用来读取jar包中或者classpath下的资源文件。以下方式都能够正确的定位文件
TestClass.class.getResource("test.txt");
TestClass.class.getResource("/net/aty/test.txt");
TestClass.class.getClassLoader().getResource("net/aty/test.txt");
Class.getResource()有2种方式,绝对路径和相对路径。绝对路径以/开头,从classpath或jar包根目录下开始搜索;
相对路径是相对当前class所在的目录,允许使用..或.来定位文件。ClassLoader.getResource()只能使用绝对路径,而且不用以/开头。
这两种方式读取资源文件,不会依赖于user.dir,也不会依赖于具体部署的环境,是推荐的做法。
3.使用Class或ClassLoader.getResource()的相对路径和绝对路径问题
无论是相对路径还是绝对路径,都是推荐的做法。考虑下这样的场景,如果a.jar中的类,需要读取b.jar中的资源文件怎么实现呢?
用相对路径和绝对路径都可以吗?
制作b.jar,并将它加入到eclipse工程的build path下,如下图
// 使用绝对对路径,正常读取b.jar中的文件
showContent(TestClass.class.getClassLoader().getResource("net/aty/test/c.txt"));
// 使用相对路径,正常读取本jar中的same.txt
showContent(TestClass.class.getResource("../same.txt"));
// 错误
showContent(TestClass.class.getResource("../a.txt"));
public static void showContent(URL url) throws Exception
BufferedReader br = new BufferedReader(new InputStreamReader(
url.openStream()));
StringBuilder contentHolder = new StringBuilder();
String lineContent = null;
while ((lineContent = br.readLine()) != null)
contentHolder.append(lineContent);
br.close();
System.out.println("content=" + contentHolder);
可以得出结论:
使用相对路径或绝对路径都能读取本jar或其他jar中的资源文件。但区别是,读取本jar包中的文件支持..这种写法,但是不能通过..读取其他jar下的文件。
4.spring框架的ClassPathResource实现
* This implementation opens an InputStream for the given class path resource.
* @see java.lang.ClassLoader#getResourceAsStream(String)
* @see java.lang.Class#getResourceAsStream(String)
public InputStream getInputStream() throws IOException {
InputStream is;
if (this.clazz != null) {
is = this.clazz.getResourceAsStream(this.path);
else {
is = this.classLoader.getResourceAsStream(this.path);
if (is == null) {
throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist");
return is;
可以看出spring提供的ClassPathResource,底层使用的就是Class.getResource或ClassLoader.getResource()。spring提供的读取文件API功能,自然是与JDK一致。
使用java读取jar或war下的配置文件,是开发者经常需要处理的事情,大家是不是经常遇到FileNotFoundException呢?java读取文件的方式也有很多,比如new File(),Class.getResource(),ClassLoader.getResource(),这些方式的差别是什么呢?开源框架struts2的ClassLoaderUtils和Spring提供ClassPat
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Outpu...
最近在工作中遇到需要读取配置文件,然后第一想法就是将文件放到项目的resources目录下,
然后使用:
String fileName = "config/zh.md"
String path = this.getClass().getResource("/").getPath() + fileName;
System.out.println(path);// D:/example/exam01/target/classes/config/zh.md
在IDE工具中开发及Debug时一切都正常,但是打
1,使用 ClassLoader.getSystemResourceAsStream(filename) :
在 普通Java项目下,你的文件与Java文件在同一包下,文件名不要加 “ / ”号
在maven 项目中,你的文件要在 resource 下,文件名参数也不要带 “ / ” 号
2,使用 java IO 中 自带的 File、 FileInputStream 、 FileReader 等
它们的 String filename 的有参构造,相对路径是从 java 工程目录开始的 , 举例
在做开发的时候,我们可能会需要从本地硬盘上读取某一个文件资源,或者修改某一个文件,这个时候就需要先找到这个文件,然后用FileInputStrem等文件字节。字符流来将这个文件读取到内存中,再对其进行修改等的操作。那么在找这个文件的过程中就涉及到一个路径的问题--->怎么正确的找到这个文件呢?
有两种可行的方法:一、绝对硬盘路径;二、相对路径
URL url = new URL(filedownload+URLEncoder.encode(filedname,"utf-8"));
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
URL 当为中文路径时,conn.getInputStream 会有 Fil
01-错误信息:
Exception in thread "main" java.io.FileNotFoundException: e:b (拒绝访问。)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOu
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileuplo
使用HttpURLConnection下载文件时经常会出现 java.io.FileNotFoundException文件找不到异常,下面介绍下解决办法
首先设置tomcat对get数据的编码:conf/server.xml
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout
文件路径正确却报java.io.filenotfoundexception异常的原因可能是以下几种情况:
1. 文件名错误:虽然文件路径是正确的,但文件名错误会导致找不到相应的文件。需要再次确认文件名是否准确无误。
2. 文件所在目录不存在:即使文件路径是正确的,如果文件所在的目录不存在,同样会导致找不到文件。需要检查文件所在的目录是否存在。
3. 文件权限问题:文件可能没有足够的权限被读取或者修改。需要检查文件的访问权限,并且确认当前用户是否有足够的权限来访问该文件。
针对以上问题,可以通过以下几个办法来解决:
1. 重新确认文件路径、文件名是否正确。尽可能使用绝对路径,可以减少误操作导致的问题。
2. 确认文件所在的目录是否存在。如果不存在,则需要创建该目录。
3. 检查文件的权限并授权给相应的用户。
4. 使用File类提供的exists()方法判断文件是否存在;或使用isFile()方法判断是否为文件。
5. 在代码中捕获FileNotFoundException异常并进行相应的处理,例如提醒用户输入有误或者进行文件复制等操作。
总之,正确、清晰的文件路径非常重要,同时需要考虑到文件本身的权限问题,才能保证程序的正常运行。