此篇文章参考java开发中文件读取方式总结 和 相对路径与绝对路径,感谢两位博主

绝对路径和相对路径

绝对路径

  • Windows 操作系统

绝对路径:是从盘符开始的路径,形如C:\windows\system32\cmd.exe。

相对路径:是从当前路径开始的路径,如当前路径为C:\windows,要描述上述路径,只需输入入system32\cmd.exe

  • LINUX操作系统

LINUX系统中 绝对路径 以“/”为起始 例:/home/user1/abc.txt。

相对路径为 : home/user1/abc.txt。

相对路径

相对路径:指由这个文件所在的路径引起的跟其它文件(或文件夹)的路径关系。其中一般以 . 和 …等符号开头的目录,或直接写下级目录文件的路径

直接写下级目录文件的路径
<a href = "jeri/index.html">index.html</a>

在Java开发中,我们通常会创建两种工程:Java Project和JavaWeb Project


Java Project

在Java Project中,我们用的是相对路径。比如我们这里有一个Java工程:工程下边会自动创建两个目录:src和bin。

  • 这时候我们如果在test下新建一个文件:nio-data.txt ,那么我们在代码当中就可以这样读取它:File f = new File(“nio-data.txt”),或者写成这样:File f = new File(“nio-data.txt”);
@Test
	public void channelTest1() throws IOException {

		
		String path = "nio-data.txt";
		System.out.println("读取项目根目录的nio-data.txt");
		RandomAccessFile aFile = new RandomAccessFile(path, "rw");
		FileChannel inChannel = aFile.getChannel();

		ByteBuffer buf = ByteBuffer.allocate(128);
		int bytesRead=0;
		//int bytesRead = inChannel.read(buf);
		while ((bytesRead=(inChannel.read(buf)))!= -1) {

			System.out.println("Read " + bytesRead);
			buf.flip();

			while (buf.hasRemaining()) {
				System.out.print((char) buf.get());
			}

			buf.clear();
			
		}
		aFile.close();
	}
  • 如果test下新建目录abc ,在abc 下新建文件nio-data.txt,代码读取就是这样:File f = new File(“abc/nio-data.txt”),或者写成这样:File f = new File(“./abc/nio-data.txt”);
//将上面代码的path修改为 
String path = "./abc/nio-data.txt";
  • 如果在src下新建文件nio-data.txt,代码读取就是这样:File f = new File(“src/nio-data.txt”),或者写成这样:File f = new File(“./src/nio-data.txt”);由于src下的所有文件最终会被编译到bin目录下,所以直接读取src下的文件没有多大意义。
//将上面代码的path修改为 
String path = "./src/nio-data.txt";

java 在当前目录创建文件并写入 在java中目录被看做_java 在当前目录创建文件并写入


  • 如果在src下有一个文件nio-data.txt,我们知道它最终被编译到bin目录下,那么怎么去读取这个编译后的文件呢?有3种方式来读取。
  1. 直接读取
@Test
	public void channelTest3() throws IOException {
		
		String path = "bin/nio-data.txt";
		System.out.println("读取bin下的nio-data.txt");
		RandomAccessFile aFile = new RandomAccessFile(path, "rw");

由于bin目录(包括web应用中WEB-INF下的classes目录)统称为classpath(类路径),对于类路径下的文件,还有以下两种读取方式

  1. 通过字节码对象读取

假如当前类为:TestDirectory.java,它所在的包是:com.mac.util在TestDirectory.java中,而nio-data.txt在src目录下

//这种以“/”开头的是以绝对路径方式读取,意思是从bin目录下开始读取nio-data.txt
String f = TestDirectory.class.getResource(“/nio-data.txt”).getPath();

//这种不以“/”开头的是以相对路径方式读取,意思是从当前类所在的目录下(com.mac.util)开始读取nio-data.txt
如果写成这样:String f = TestDirectory.class.getResource(“nio-data.txt”).getPath();

//还可以这样读取
String path = TestDirectory.class.getResource("../../../nio-data.txt").getPath();

==综上所述:采用相对路径读取文件还要考虑当前类所在的位置,非常麻烦,所以对于classpath下的文件,我们统一采用绝对路径方式来读取。绝对路径貌似就是以bin目录为根节点

  1. 通过类加载器读取

假如当前类为:TestDirectory.java,它所在的包是:com.mac.util在TestDirectory.java中,而nio-data.txt在src目录下

String path = TestDirectory.class.getClassLoader().getResource("nio-data.txt").getPath();

如果使用绝对路径就读取不到了
String path = TestDirectory.class.getClassLoader().getResource("/nio-data.txt").getPath();

如果把nio-data.txt放到com.mac.util包下
String path = TestDirectory.class.getClassLoader().getResource("com/mac/util/nio-data.txt").getPath();

可以看出,通过classloader读的话,它只会从classpath下开始读取,并且不能以“/”开头。

JavaWeb Project

在JavaWeb Project中,使用相对路径是读不到文件的,因为JavaWeb Project与Java Project不同,项目最终要部署在web服务器中,文件只能使用绝对路径来读取。
比如我使用的IDE是eclipse,建立一个web project:day01,在WebContent(工程根目录)下有一个文件1.txt,那么就可以通过ServletContext对象来读取了。

//比如在servlet中,可以这样:这种方式只是获取文件的路径字符串,并不会检查是否真的存在这样一个文件
getServletConfig().getServletContext().getRealPath(“/1.txt”);

//比如我把1.txt删除,执行上边代码仍然可以打出路径:...../apache-tomcat-7.0.42/webapps/day01/1.txt。并且这里的路//径可以是绝对路径也可以是相对路径,取得的效果是一样的。比如写成这样也是可以的:
getServletConfig().getServletContext().getRealPath(“1.txt”);

//如果在WEB-INF下有一个文件2.txt,读取:
getServletConfig().getServletContext().getRealPath(“/WEB-INF/2.txt”);
//也就是说通过ServletContext对象可以获取web工程根目录及其子目录(包括WEB-INF、classes等)所有文件的绝对路径。

当然,如果文件位于classpath(也就是classes目录)下,我们还可以通过另两种方式来读取。

  • 通过字节码对象来读
    这种方式与上边Java Project中通过字节码对象读取类路径下文件的方式相同,可参照上边的4.2。
  • 通过类加载器来读
    这种方式与上边Java Project中通过classloader读取类路径下文件的方式基本相同,可参照上边的4.3。 唯一不同的是:上边的路径只能是相对路径,也就是说不能以“/”开头。而在JavaWeb Project中,可以是相对路径,也可以是绝对路径,也就说你加不加“/”开头效果是一样的。