java learnging

          一塊探討JAVA的奧妙吧
          posts - 34, comments - 27, trackbacks - 0, articles - 22

          文件路徑的操作

          Posted on 2005-04-15 13:57 bigseal 閱讀(343) 評論(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文件得到的文件路徑是不一樣的。
          不知道為什么會出現這中情況呢?

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 布拖县| 黄陵县| 措勤县| 鹤峰县| 星子县| 铜川市| 河北省| 漳州市| 泸定县| 平山县| 扶余县| 鹰潭市| 信阳市| 邵阳县| 项城市| 闽清县| 青阳县| 乾安县| 潼关县| 上林县| 怀远县| 和平区| 榆林市| 临朐县| 东辽县| 会泽县| 莲花县| 塔城市| 齐河县| 关岭| 登封市| 巴林左旗| 仪陇县| 博客| 深圳市| 吉木萨尔县| 柳江县| 横山县| 华阴市| 会宁县| 旬阳县|