javainthinking

          XL的個性空間和java學習之旅

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            13 隨筆 :: 13 文章 :: 20 評論 :: 1 Trackbacks

          package com.xionglian.common;

          import java.io.File;
          import java.net.URL;
          import java.io.InputStream;
          import java.io.IOException;
          import java.io.BufferedWriter;
          import java.io.FileWriter;

          /**
           * <p>Title: </p>
           * <p>Description: </p>
           * <p>Copyright: Copyright (c) 2000</p>
           * <p>Company: </p>
           * @author 熊煉
           * @version 1.0
           */

          public class FileHandle
          {

            public FileHandle()
            {
            }

            /**
             * filename path和URL之間的轉換
             */
            public void _File_URL()
            {
              String path = File.separator + "a";
              System.out.println(path);
              //coverting Between a filename and a URL
              //create a file object
              File file = new File("filename");

              //covert a file object to a URL
              URL url = null;
              try
              {
                url=file.toURL();
                System.out.println(url);
              }
              catch(Exception  e)
              {
                e.printStackTrace();
              }

              //convert a URL to a file object
              file=new File(url.getFile());

              //read the file using the URL
              try
              {
                InputStream is=url.openStream();
                //read from the InputStream
              }
              catch(IOException e)
              {
                e.printStackTrace();
              }
            }

            /**
             * 從relative path到absolute path之間的轉換
             */
            public void rel_to_abspath()
            {
              File file=new File("filename.txt");//filename.txt doesn't need exist
              file=file.getAbsoluteFile();//C:\Documents and Settings\xl\桌面\First\filename

              file=new File("test"+File.separator+"filename.txt");
              file=file.getAbsoluteFile();//C:\Documents and Settings\xl\桌面\First\test\filename.txt
            }

            /**
             * 判斷兩個filename path是否指向同一file
             * @return boolean
             */
            public boolean isSameFile()
            {
              File file1=new File("filename");
              File file2=new File("/filename");
              //file1.equals(file2)=false;
              try
              {
                //利用File.getCanonicalFile()消除多余的symbol links或者"."和".."
                file1.getCanonicalFile();
                file2.getCanonicalFile();
              }
              catch(Exception e){}
              return file1.equals(file2);//true
            }

            /**
             * 創建一個臨時temporary文件
             * @param args
             */
            public void createTempFile()
            {
              try
                {
                  //create temporary file
                  File temp = new File("temp", ".txt");
                  //推出程序時刪除temp文件
                  temp.deleteOnExit();

                  //向temp文件中寫content
                  BufferedWriter out = new BufferedWriter(new FileWriter(temp));
                  out.write("熊煉");
                  out.close();
                }
                catch(IOException e)
                {
                  e.printStackTrace();
                }
            }

            /**
             * File、directory更名或File移動到另一個位置
             * @param args
             */
            public void rename_move_File()
            {
              //rename a File or directory
              File file1=new File("oldname");
              File file2=new File("newname");
              file1.renameTo(file2);

              //move File
              File file=new File("filename");
              File dir=new File("directoryname");
              file.renameTo(new File(dir,file.getName()));
            }

            public static void main(String[] args)
            {

            }

           

            }

          posted on 2005-07-27 09:07 XL的個性空間和java學習之旅 閱讀(2117) 評論(0)  編輯  收藏 所屬分類: 實用操作類庫common

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


          網站導航:
           
          主站蜘蛛池模板: 嘉定区| 黄冈市| 潜江市| 乳山市| 长白| 宣化县| 芜湖市| 大石桥市| 涡阳县| 建平县| 五家渠市| 定安县| 扶沟县| 泊头市| 灵武市| 信宜市| 静乐县| 邓州市| 玛曲县| 仙游县| 南江县| 大兴区| 无锡市| 邵东县| 嵩明县| 女性| 永和县| 建平县| 吉林省| 鹤壁市| 翁源县| 什邡市| 梅州市| 平原县| 余姚市| 调兵山市| 合阳县| 泸西县| 蒙城县| 棋牌| 随州市|