ITeye博客

使用java复制移动文件

v64500 · 2008-08-15 17:02 · java · Java · Markdown

移动文件


    public static boolean move(String srcFile, String destPath){
        // File (or directory) to be moved
        File file = new File(srcFile);
       
        // Destination directory
        File dir = new File(destPath);
       
        // Move file to new directory
        boolean success = file.renameTo(new File(dir, file.getName()));
       
        return success;
    }

复制文件


private void copyFile(File in, File out) throws Exception{
        FileChannel sourceChannel= new FileInputStream(in).getChannel();
        FileChannel destinationChannel= new FileOutputStream(out).getChannel();
        sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
        // or
        // destinationChannel.transferFrom(sourceChannel, 0,
        // sourceChannel.size());
        sourceChannel.close();
        destinationChannel.close();
        sourceChannel= null;
        destinationChannel= null;
    }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。