用python, c#, java写的文件拷贝

来源:互联网 发布:弄璋乎弄瓦乎原文 编辑:程序博客网 时间:2024/06/05 15:41

     前一段时间试着用这三种语言简单的写了关于文件拷贝的程序,发现c#和python的api惊人的相似,对于文件的操作这两种语言非常的方便。都没有加异常的处理

C#源代码:

        public static void CopyFile(string source, string destination)
        {
            if (string.IsNullOrEmpty(source) | string.IsNullOrEmpty(destination))
            {
                throw new ArgumentNullException("传入的目录不存在");
            }

            if (IsDirectory(source))
            {
                if (IsDirectory(destination))
                {
                    var currentFiles = Directory.GetFiles(source);
                    if (currentFiles.Length > 0)
                    {
                        foreach (var sourceFile in currentFiles)
                        {
                            var fileName = sourceFile
                                .Substring(sourceFile.LastIndexOf('//') + 1);
                            var creatFile = Path.Combine(destination, fileName);
                            File.Copy(sourceFile, creatFile, true);
                        }
                    }

                    var currentDir = Directory.GetDirectories(source);
                    if (currentDir.Length > 0)
                    {
                        foreach (var dir in currentDir)
                        {
                            var dirName = dir.Substring(dir.LastIndexOf('//') + 1);
                            var path = Path.Combine(destination, dirName);
                            var creatDir = Directory.CreateDirectory(path);
                        }
                    }
                }
                else
                {
                    throw new Exception("拷贝文件的目的地路径不正确,不能将目录拷贝到非目录中");
                }
            }
            else
            {
                if (IsDirectory(destination))
                {
                    var fileName = source.Substring(source.LastIndexOf('//') + 1);
                    File.Copy(source, destination + fileName);
                }
                else
                {
                    var desName = destination.Substring(destination.LastIndexOf('//') + 1);
                    File.Copy(source, desName, true);
                }
            }
        }

        public static bool IsDirectory(string path)
        {
            if (Directory.Exists(path))
                return true;
            else
                return false;
        }

java源码:

public static void fileCopy(String source, String destination)
            throws Exception {

        File sourceFile = new File(source);
        if (sourceFile.exists() && sourceFile.canRead()) {
            if (!isFile(source)) {
                if (isFile(destination)) {
                    throw new Exception("目标应该是文件夹");
                } else {
                    File[] sourceFiles = sourceFile.listFiles();
                    for (File file : sourceFiles) {
                        String fileName = file.getName();
                        String desFileName;
                        if (!destination.endsWith(System
                                .getProperty("file.separator")))
                            desFileName = destination
                                    + System.getProperty("file.separator")
                                    + fileName;
                        else
                            desFileName = destination + fileName;
                        if (file.isFile()) {
                            copyFile(file.getName(), desFileName);
                        } else if (file.isDirectory()) {
                            createDirectory(desFileName);
                            fileCopy(source
                                    + System.getProperty("file.separator")
                                    + file.getName(), desFileName);
                        }
                    }
                }
            } else {

                if (isFile(destination)) {
                    copyFile(source, destination);
                } else {
                    String desName = source.substring(source.lastIndexOf(System
                            .getProperty("file.separator")));
                    // 注意
                    copyFile(source, destination
                            + System.getProperty("file.separator") + desName);
                }

            }
        } else {
            throw new Exception("传入参数非法");
        }
    }

    public static boolean isFile(String path) {
        File file = new File(path);
        if (file.isFile())
            return true;
        else
            return false;
    }

    public static void copyFile(String from, String to) {
        try {
            FileInputStream fFrom;
            FileOutputStream fTo;
            File file = new File(to);

            if (file.isFile()) {
                fTo = new FileOutputStream(to);
            } else {
                String fileName = new File(from).getName();

                new File(fileName).createNewFile();
                fTo = new FileOutputStream(to+fileName);
            }
            fFrom = new FileInputStream(from);

            byte bt[] = new byte[1024];
            int c;
            while ((c = fFrom.read(bt)) > 0) {
                fTo.write(bt, 0, c);
            }
            fFrom.close();
            fTo.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean createDirectory(String dirName) {
        return new File(dirName).mkdir();
    }

python源码:

def file_copy(source, destination):
    if(os.path.isdir(source)):
        if(os.path.isdir(destination)):
            currentFiles = os.listdir(source)
            for file in currentFiles:
                currentFile = os.path.join(source, file)
                destFile = os.path.join(destination, file)
                if(os.path.isfile(currentFile)):
                    shutil.copyfile(currentFile, destFile)
                else:
                    os.mkdir(destFile)
                    file_copy(currentFile, destFile)               
        else:
            print "The destination is not a directory or not exit"
    else:
        if(os.path.isdir(destination)):
            fileName = os.path.split(source)[1]
            desName = os.path.join(destination, fileName)
            shutil.copy(source, desName)
        else:
            shutil.copy(source, destination)