java

位置:IT落伍者 >> java >> 浏览文章

JAVA实现文件转移


发布日期:2023年06月08日
 
JAVA实现文件转移

Java代码

/**

* //从旧文件拷贝内容到新文件

* //删除旧文件

* @param oldPath the path+name of old file

* @param newPath the path+name of new file

* @throws Exception

*/

private void transferFile(String oldPathString newPath) throws Exception {

int byteread = ;

File oldFile = new File(oldPath);

FileInputStream fin = null;

FileOutputStream fout = null;

try{

if(oldFileexists()){

fin = new FileInputStream(oldFile);

fout = new FileOutputStream(newPath);

byte[] buffer = new byte[];

while( (byteread = finread(buffer)) != ){

loggerdebug(byteread==+byteread);

foutwrite(bufferbyteread);

}

if(fin != null){

finclose();//如果流不关闭则删除不了旧文件

thisdelFile(oldFile);

}

}else{

throw new Exception(需要转移的文件不存在!);

}

}catch(Exception e){

eprintStackTrace();

throw e;

}finally{

if(fin != null){

finclose();

}

}

}

/**

* 删除文件只支持删除文件不支持删除目录

* @param file

* @throws Exception

*/

private void delFile(File file) throws Exception {

if(!fileexists()) {

throw new Exception(文件+filegetName()+不存在请确认!);

}

if(fileisFile()){

if(filecanWrite()){

filedelete();

}else{

throw new Exception(文件+filegetName()+只读无法删除请手动删除!);

}

}else{

throw new Exception(文件+filegetName()+不是一个标准的文件有可能为目录请确认!);

}

}

               

上一篇:Java 理论与实践: 正确使用 volatile 变量 线程同步

下一篇:Java爱好者菜园培训:JSP及语法概要