文件移动与下载

/**
     * 
     * 移动文件
     * @param oldFileName 旧文件的全路径 
     * @param newFilePath 目标文件夹
   * @param newFileName 新文件的全路径   *
@see [类、类#方法、类#成员] */ public static void moveFile(String oldFileName, String newFilePath) { File oldFile = new File(oldFileName); File fnewpath = new File(newFilePath); if (!fnewpath.exists()) fnewpath.mkdirs(); File fnew = new File(new StringBuffer().append(newFilePath).append(oldFile.getName()).toString()); oldFile.renameTo(fnew); } public static void moveFile(String oldFileName, String newFilePath,String newFileName) { File oldFile = new File(oldFileName); File fnewpath = new File(newFilePath); if (!fnewpath.exists()) fnewpath.mkdirs(); File fnew = new File(new StringBuffer().append(newFilePath).append(newFileName).toString()); oldFile.renameTo(fnew); }
/**
     * 下载文件
     * @param urlString 文件地址
     * @param filename 保存文件名
     * @param savePath 保存文件夹
     * @throws Exception
     * @see [类、类#方法、类#成员]
     */
    public static void download(String urlString, String filename, String savePath)
        throws Exception
    {
        URL url = new URL(urlString);
        URLConnection con = url.openConnection();
        con.setConnectTimeout(5 * 1000);
        InputStream is = con.getInputStream();
        byte[] bs = new byte[1024];
        int len;
        File sf = new File(savePath);
        if (!sf.exists())
        {
            sf.mkdirs();
        }
        OutputStream os = new FileOutputStream(sf.getPath() + File.separator + filename);
        while ((len = is.read(bs)) != -1)
        {
            os.write(bs, 0, len);
        }
        os.close();
        is.close();
    }

 

posted @ 2016-01-04 15:30  欧文_Owen  阅读(155)  评论(0编辑  收藏  举报