思路:
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; //字節(jié)數(shù) 文件大小
//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();
}
}
(責任編輯:代碼如詩) |