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学习之旅 阅读(2097) 评论(0)  编辑  收藏 所属分类: 实用操作类库common

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


网站导航: