java,php,asp.net,linux,javascript,mysql,mssql,oracle,编程

拷贝一个目录或者文件到指定路径下

转载请注明:http://www.pmjava.com/Article/ShowInfo.asp?ID=56566
/**
     * 拷贝一个目录或者文件到指定路径下
     *
     *
@param source
     *
@param target
    
*/
   
public static void copy(File source, File target)
    {
        File tarpath
= new File(target, source.getName());
       
if (source.isDirectory())
        {
            tarpath.mkdir();
            File[] dir
= source.listFiles();
           
for (int i = 0; i < dir.length; i++) { copy(dir[i], tarpath); } } else
        {
           
try
            {
                InputStream is
= new FileInputStream(source);
                OutputStream os
= new FileOutputStream(tarpath);
               
byte[] buf = new byte[1024];
               
int len = 0;
               
while ((len = is.read(buf)) != -1)
                {
                    os.write(buf,
0, len);
                }
                is.close();
                os.close();
            }
           
catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
           
catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

                                                                  

posted on 2009-06-10 21:19 rrong_m 阅读(482) 评论(0)  编辑  收藏

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

随笔档案

文章分类

文章档案

java编程

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜