java learnging

一块探讨JAVA的奥妙吧
posts - 34, comments - 27, trackbacks - 0, articles - 22

文件路径的操作

Posted on 2005-04-15 13:57 bigseal 阅读(335) 评论(0)  编辑  收藏

    看一些开源代码的时候发展自己对关于文件路径的操作的使用不甚理解,就把它们放到一块对比着看一下。

import java.io.IOException;
import java.net.URL;

/**
 * @author Administrator
 * @version 1.0
 * 
 * TODO
 
*/

public class Simple {

    
public static void main(String[] args) {
        
        System.
out.println("getResource");

        System.
out.println(Simple.class.getResource(""));
        System.
out.println(Simple.class.getResource("."));
        System.
out.println(Simple.class.getResource("/"));
        System.
out.println(Simple.class.getResource("/images/About.gif"));
        System.
out.println(Simple.class.getResource("images/About.gif"));
        
        System.
out.println("getAbsolutePath:");
        
        System.
out.println(new File("").getAbsolutePath());
        System.
out.println(new File(".").getAbsolutePath());
        System.
out.println(new File("/").getAbsolutePath());
        System.
out.println(new File("images/About.gif").getAbsolutePath());
        System.
out.println(new File("/images/About.gif").getAbsolutePath());
        
    }

}
这是在eclipse中显示的运行结果
getResource
file:/D:/software/eclipse%203.0.1/workspace/simple/bin/simple/
file:/D:/software/eclipse%203.0.1/workspace/simple/bin/simple/
file:/D:/software/eclipse%203.0.1/workspace/simple/j2src/
file:/D:/software/eclipse%203.0.1/workspace/simple/bin/images/About.gif
null
getAbsolutePath:
D:\software\eclipse 3.0.1\workspace\simple
D:\software\eclipse 3.0.1\workspace\simple\.
D:\
D:\software\eclipse 3.0.1\workspace\simple\images\About.gif
D:\images\About.gif
这是在命令行下运行class文件的结果
getResource
file:/D:/software/eclipse%203.0.1/workspace/simple/bin/simple/
file:/D:/software/eclipse%203.0.1/workspace/simple/bin/simple/
file:/D:/software/eclipse%203.0.1/workspace/simple/bin/
file:/D:/software/eclipse%203.0.1/workspace/simple/bin/images/About.gif
null
getAbsolutePath:
D:\software\eclipse 3.0.1\workspace\simple\bin
D:\software\eclipse 3.0.1\workspace\simple\bin\.
D:\
D:\software\eclipse 3.0.1\workspace\simple\bin\images\About.gif
D:\images\About.gif
这是项目的文件夹结构
simple 
      ├─bin
      │  ├─images
      │  └─simple
      └─src
            ├─images
            └─simple
可以看到有时候分别在elipse运行和命令行class文件得到的文件路径是不一样的。
不知道为什么会出现这中情况呢?

只有注册用户登录后才能发表评论。


网站导航: