java中文件拷贝的代码 java中文件拷贝的代码详解

JAVA实现文件转移 Java代码
【java中文件拷贝的代码 java中文件拷贝的代码详解】 /**
* // 从旧文件拷贝内容到新文件
* // 删除旧文件
* @param oldPath the path+name of old file
* @param newPath the path+name of new file
* @throws Exception
*/
private void transferFile(String oldPath String newPath) throws Exception {
int byteread = ;
File oldFile = new File(oldPath);
FileInputStream fin = null;
FileOutputStream fout = null;
try{
if(oldFile exists()){
fin = new FileInputStream(oldFile);
fout = new FileOutputStream(newPath);
byte[] buffer = new byte[ ];
while( (byteread = fin read(buffer)) != ){
logger debug( byteread== +byteread);
fout write(buffer byteread);
}
if(fin != null){
fin close();//如果流不关闭 则删除不了旧文件
this delFile(oldFile);
}
}else{
throw new Exception( 需要转移的文件不存在! );
}
}catch(Exception e){
e printStackTrace();
throw e;
}finally{
if(fin != null){
fin close();
}
}
}
/**
* 删除文件 只支持删除文件 不支持删除目录
* @param file
* @throws Exception
*/
private void delFile(File file) throws Exception {
if(!file exists()) {
throw new Exception( 文件 +file getName()+ 不存在 请确认! );
}
if(file isFile()){
if(file canWrite()){
file delete();
}else{
throw new Exception( 文件 +file getName()+ 只读 无法删除 请手动删除! );
}
}else{
throw new Exception( 文件 +file getName()+ 不是一个标准的文件 有可能为目录 请确认! );
}
lishixinzhi/Article/program/Java/hx/201311/25584
利用JAVA语言编写一个 名为copy的程序 实现文件的拷贝功能,应该怎样做?import java.io.File;\x0d\x0aimport java.io.FileInputStream;\x0d\x0aimport java.io.FileNotFoundException;\x0d\x0aimport java.io.FileOutputStream;\x0d\x0aimport java.io.IOException;\x0d\x0apublic class Copy {\x0d\x0a/**\x0d\x0a* @param args\x0d\x0a*/\x0d\x0apublic static void main(String[] args) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0aif(args.length!=2){\x0d\x0aSystem.out.print("没有输入正确数目的参数,程序退出!");\x0d\x0aSystem.exit(0);\x0d\x0a}\x0d\x0aFile fileS = new File("./"+args[0]);\x0d\x0aFile fileD = new File("./"+args[1]);\x0d\x0aif(fileD.exists())System.out.println("目标文件 "+args[1]+" 已存在!");\x0d\x0abyte[] temp = new byte[50];\x0d\x0aint totalSize = 0;\x0d\x0atry {\x0d\x0aFileInputStream fr = new FileInputStream(fileS);\x0d\x0aFileOutputStream fo = new FileOutputStream(fileD);\x0d\x0aint length = 0;\x0d\x0awhile((length = fr.read(temp, 0, temp.length)) != -1){\x0d\x0atotalSize += length;\x0d\x0afo.write(temp, 0, length);\x0d\x0a}\x0d\x0aSystem.out.println("文件 "+args[0]+" 有 "+totalSize+" 个字节");\x0d\x0aSystem.out.println("复制完成!");\x0d\x0a} catch (FileNotFoundException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0aSystem.out.println("源文件 "+args[0]+" 不存在!");\x0d\x0a} catch (IOException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}
怎样用java程序实现文件拷贝通过输入输出流解决此问题 , 具体的可以查看JDK的API,实在不会的话,百度一下应该都有一堆这方面的代码 。
关于java中文件拷贝的代码和java中文件拷贝的代码详解的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读