思路:
1.copy
2.delete
示例:
/**
* 复制单个文件
* @param String oldPath 原文件路径 如:c:/a.txt
* @param String newPath 复制后路径 如:f:/a.txt
*/
public static void copyFile(String oldPath, String newPath)
{
try
{
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists())
{ //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1)
{
bytesum += byteread; //字节数 文件大小
//System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e)
{
//System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
/**
* 删除单个文件
* @param String Path 文件路径
* @param String filename 文件名
*/
public static void delFile(String Path, String filename)
{
try{
String path=application.getRealPath(Path);
File fobj=new File(path,filename);
boolean bo_Return=fobj.delete();
}
catch (Exception e)
{
//System.out.println("删除单个文件操作出错");
e.printStackTrace();
}
}
(责任编辑:代码如诗) |