中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 基础 > Java语言基础
目录文件的操作
作者:佚名 时间:2005-07-23 15:08 出处:互连网 责编:chinaitpower
              摘要:目录文件的操作

目录文件的操作也是除了数据库操作以外,经常需要操作的一个数据对象.

移动文件,相当于linux 中mv命令,但与平台无关:

/**
* This class moves an input file to output file
*
* @param String input file to move from
* @param String output file
*
*/
public static void move (String input, String output){
  File inputFile = new File(input);
  File outputFile = new File(output);
  inputFile.renameTo(outputFile);
}

 

拷贝文件,相当于linux中cp命令,但与平台无关,可以拷贝文本 或二进制文件:

/**
* This class copies an input file to output file
*
* @param String input file to copy from
* @param String output file
*/
public static boolean copy(String input, String output) throws Exception{
  int BUFSIZE = 65536;
  try{
    FileInputStream fis = new FileInputStream(input);
    FileOutputStream fos = new FileOutputStream(output);

    int s;
    byte[] buf = new byte[BUFSIZE];
    while ((s = fis.read(buf)) > -1 ){
      fos.write(buf, 0, s);
    }

  }catch (Exception ex) {
    throw new Exception("makehome"+ex.getMessage());
  }
  return true;
}

 

拷贝目录,下例只可以拷贝目录中的文件:

/**
* This class copies an input files of a directory to another directory not include subdir
*
* @param String sourcedir the directory to copy from such as:/home/bqlr/images
* @param String destdir the target directory
*/
public static void CopyDir(String sourcedir,String destdir) throws Exception
{
  File dest = new File(destdir);
  File source = new File(sourcedir);

  String [] files= source.list();
  try
  {
    destdir.mkdirs();
  }catch (Exception ex) {
    throw new Exception("CopyDir:"+ex.getMessage());
  }


  for (int i = 0; i < files.length; i++)
  {
    String sourcefile = source+File.separator+files[i];
    String destfile = dest+File.separator+files[i];
    File temp = new File(sourcefile);
    if (temp.isFile()){
      try{
        copy(sourcefile,destfile);
      }catch (Exception ex) {
        throw new Exception("CopyDir:"+ex.getMessage());
      }
    }
  }
}

删除目录.包括目录下所有文件和目录:

/**
* This class del a directory recursively,that means delete all files and directorys.
*
* @param File directory the directory that will be deleted.
*/
public static void recursiveRemoveDir(File directory) throws Exception
{
  if (!directory.exists())
    throw new IOException(directory.toString()+" do not exist!");
  String[] filelist = directory.list();
  File tmpFile = null;
  for (int i = 0; i < filelist.length; i++) {
    tmpFile = new File(directory.getAbsolutePath(),filelist[i]);
    if (tmpFile.isDirectory()) {
      recursiveRemoveDir(tmpFile);
    } else if (tmpFile.isFile()) {
      tmpFile.delete();
    }
  }
  directory.delete();
}

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有