巷尾的酒吧

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  64 Posts :: 0 Stories :: 5 Comments :: 0 Trackbacks
package com.abin.test.result;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
//以前做Java很少会用到文件,原因之一是我做的项目的那一块很少用到文件操作,只用过文件上传,应我同学的要求,今天帮他实现了一下,放在这里

//以备以后再用的着的话好找,这些都是参考网上好多人的心血而成的。
public class CopyFile1 {
//复制文件并且改名:
    
public static void doCopyFile(String fromPath ,String toPath) throws IOException{
        String toNewPath
=toPath+fromPath.subSequence(fromPath.lastIndexOf("\\")+1, fromPath.length());
        File srcFile 
= new File(fromPath);
        File destFile 
= new File(toNewPath);
        FileInputStream input 
= new FileInputStream(srcFile);
        
try {
            FileOutputStream output 
= new FileOutputStream(destFile);
            
try {
                
byte[] buffer = new byte[4096];
                
int n = 0;
                
while (-1 != (n = input.read(buffer))) {
                    output.write(buffer, 
0, n);
                }
            } 
finally {
                
try {
                    
if (output != null) {
                        output.close();
                    }
                } 
catch (IOException ioe) {
                    
// ignore
                }
            }
        } 
finally {
            
try {
                
if (input != null) {
                    input.close();
                }
            } 
catch (IOException ioe) {
                
// ignore
            }
        }
    }
//移动文件并且改名:
    
public static void doCopyFileAndUpdateFileName(String fromPath ,String toPath,String newName) throws 

IOException{
        String toNewPath
=toPath+newName;
        File srcFile 
= new File(fromPath);
        File destFile 
= new File(toNewPath);
        FileInputStream input 
= new FileInputStream(srcFile);
        
try {
            FileOutputStream output 
= new FileOutputStream(destFile);
            
try {
                
byte[] buffer = new byte[4096];
                
int n = 0;
                
while (-1 != (n = input.read(buffer))) {
                    output.write(buffer, 
0, n);
                }
            } 
finally {
                
try {
                    
if (output != null) {
                        output.close();
                    }
                } 
catch (IOException ioe) {
                    
// ignore
                }
            }
        } 
finally {
            
try {
                
if (input != null) {
                    input.close();
                }
            } 
catch (IOException ioe) {
                
// ignore
            }
        }
    }
//删除文件:
    
public static void doDeleteFile(String path) throws IOException{
        File file
=new File(path);
        
if(file.exists()||file.isFile()||!file.isDirectory()){
            file.delete();
            file.deleteOnExit();
        }
    }
//修改文件名:
    
public static void doUpdateFileName(String path1,String newName1) throws IOException{
        String path
=path1.substring(0, path1.lastIndexOf("\\"))+"\\";
        System.out.println(path);
        File newFile
=new File(path+newName1);
        File file
=new File(path1);
        file.renameTo(newFile);
           
    }
    
public static void main(String args[])throws Exception {
        String fromPath
="e:\\love.jpg";
        String toPath
="e:\\demo\\";
        String newName
="abin.jpg";
        String newName1
="abin.jpg";
        String path
="e:\\demo\\love.jpg";
        String path1
="e:\\demo\\love.jpg";
    
//    new CopyFile1().doCopyFile(fromPath, toPath);
    
//    new CopyFile1().doCopyFileAndUpdateFileName(fromPath, toPath, newName);
    
//    new CopyFile1().doDeleteFile(path);
        new CopyFile1().doUpdateFileName(path1, newName1);
        
        CopyFile1 aaa
=new CopyFile1();
        aaa.doCopyFile(fromPath, toPath);
        
        
    }

}
posted on 2011-09-28 23:24 abing 阅读(235) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: