用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)
- 用python, c#, java写的文件拷贝
- 写个python 脚本实现文件的递归拷贝
- 用python代码实现文件的拷贝
- java文件的拷贝
- Java 文件的拷贝
- Java NIO读,写,拷贝文件
- 用java实现文件的拷贝
- 习题:用java实现文件的拷贝
- shell 脚本写的拷贝大文件显示拷贝百分比
- C++::浅拷贝,深拷贝,引用计数的拷贝,写时拷贝
- 用java写了一个实用的类----递归的拷贝给定目录的.java文件到指定的一个文件中
- C++写文件拷贝
- JAVA写的一个文件夹的文件拷贝另一个文件夹的下
- 用c写python的拓展模块
- 用python写wps的excel文件
- 用C语言实现一个自己的文件拷贝工具
- C/C++:拷贝文件
- python 实现文件的递归拷贝-转
- 设计模式学习与实践(六)----抽象工厂模式(abstract factory)
- MS SQL Server和MySQL区别
- VC常用数据类型使用转换详解
- 如何使用.NET生成C#源代码
- 2010年1月21-22日 中文ScrumMaster课程
- 用python, c#, java写的文件拷贝
- BugFree 2使用帮助
- reload 方法 replace 方法
- oracle sql语言模糊查询--通配符like的使用教程
- http header详解
- 一个星期编写 antenna
- 设计模式学习与实践(七)----建造模式(builder)
- 专门针对delphi的,嵌入源码的病毒(转)
- ORACLE SQL优化