copyfile.java
来自「一套完整的档案管理系统」· Java 代码 · 共 119 行
JAVA
119 行
package com.stsc.archive.backup;
/**
拷贝文件
*/
import java.io.RandomAccessFile;
import java.io.File;
public final class CopyFile
{
/**
将sourcefile文件复制到newfile文件
*/
public static final boolean copy(String newfile, String sourcefile)
{
if( (sourcefile == null) || (sourcefile.length() < 1)
|| (newfile == null) || (newfile.length() < 1) )
{
return false;
}
try
{
//如果不存在目录,则创建目录
createDir(newfile);
//=========================
RandomAccessFile srcfile = new RandomAccessFile(sourcefile, "r");
RandomAccessFile dstfile = new RandomAccessFile(newfile, "rw");
long leng = srcfile.length();
byte[] bData = new byte[(int)leng];
srcfile.read(bData, 0, (int)leng);
srcfile.close();
dstfile.write(bData, 0, (int)leng);
dstfile.close();
return true;
}catch(Exception e)
{
String err=e.toString();
System.out.println(err);
return false;
}
}
//获得文件名中的路径
public static final String getDir(String filename)
{
if(filename == null)
return null;
String path = null;
int index = filename.lastIndexOf("\\");
if(index > 0)
{
path = new String(filename);
System.out.println("path = "+path);
path = path.substring(0, index);
System.out.println("sub path = "+path);
}
else
{
int index1 = filename.lastIndexOf("//");
if(index1 > 0)
{
path = new String(filename);
System.out.println("path = "+path);
path = path.substring(0, index1);
System.out.println("sub path = "+path);
}
}
return path;
}
//创建文件名中的路径
public static final boolean createDir(String filename)
{
//如果不存在目录,则创建目录
String path = getDir(filename);
return mkDir(path);
/* if(path != null)
{
File dir = new File(path);
if (!dir.exists())
{
boolean result = dir.mkdirs();
if(result == false)
{
System.out.println("创建目录失败");
}
}
}*/
}
public static final boolean mkDir(String path)
{
if(path == null)
return false;
File dir = new File(path);
if (!dir.exists())
{
boolean result = dir.mkdirs();
if(result == false)
{
System.out.println("创建目录失败");
return false;
}
}
return true;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?