java移动/赋值文件 copy/move file

时间:2022-04-22
本文章向大家介绍java移动/赋值文件 copy/move file,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 1 public class FileAccess
 2 {
 3 
 4  public static boolean Move(File srcFile, String destPath)
 5  {
 6         // Destination directory
 7         File dir = new File(destPath);
 8       
 9         // Move file to new directory
10         boolean success = srcFile.renameTo(new File(dir, srcFile.getName()));
11       
12         return success;
13     }
14  
15  public static boolean Move(String srcFile, String destPath)
16  {
17         // File (or directory) to be moved
18         File file = new File(srcFile);
19       
20         // Destination directory
21         File dir = new File(destPath);
22       
23         // Move file to new directory
24         boolean success = file.renameTo(new File(dir, file.getName()));
25       
26         return success;
27     }
28  
29  public  static   void     Copy(String     oldPath,     String     newPath)   
30    {   
31           try     {   
32                   int     bytesum     =     0;   
33                   int     byteread     =     0;   
34                   File     oldfile     =     new     File(oldPath);   
35                   if     (oldfile.exists())     {     
36                           InputStream     inStream     =     new     FileInputStream(oldPath);    
37                           FileOutputStream     fs     =     new     FileOutputStream(newPath);   
38                           byte[]     buffer     =     new     byte[1444];   
39                           int     length;   
40                           while     (     (byteread     =     inStream.read(buffer))     !=     -1)     {   
41                                   bytesum     +=     byteread;       
42                                   System.out.println(bytesum);   
43                                   fs.write(buffer,     0,     byteread);   
44                           }   
45                           inStream.close();   
46                   }   
47           }   
48           catch     (Exception     e)     {   
49                   System.out.println( "error  ");   
50                   e.printStackTrace();   
51           }   
52     }    
53    public   static  void     Copy(File     oldfile,     String     newPath)   
54    {   
55           try     {   
56                   int     bytesum     =     0;   
57                   int     byteread     =     0;   
58                   //File     oldfile     =     new     File(oldPath);   
59                   if     (oldfile.exists())     {     
60                           InputStream     inStream     =     new     FileInputStream(oldfile);    
61                           FileOutputStream     fs     =     new     FileOutputStream(newPath);   
62                           byte[]     buffer     =     new     byte[1444];   
63                           while     (     (byteread     =     inStream.read(buffer))     !=     -1)     {   
64                                   bytesum     +=     byteread;       
65                                   System.out.println(bytesum);   
66                                   fs.write(buffer,     0,     byteread);   
67                           }   
68                           inStream.close();   
69                   }   
70           }   
71           catch     (Exception     e)     {   
72                   System.out.println( "error  ");   
73                   e.printStackTrace();   
74           }   
75     }    
76 }

自己做了个demo

 1 import java.io.*;
 2 public class FileAccess {
 3  public  static   void     Copy(String     oldPath,     String     newPath)   
 4        {   
 5               try     {   
 6                       int     bytesum     =     0;   
 7                       int     byteread     =     0;   
 8                       File    oldfile     =     new     File(oldPath);   
 9                       if     (oldfile.exists())     {     
10                               InputStream     inStream     =     new     FileInputStream(oldPath);    
11                               FileOutputStream     fs     =     new     FileOutputStream(newPath);   
12                               byte[]     buffer     =     new     byte[1444];   
13                               int     length;   
14                               while     (     (byteread     =     inStream.read(buffer))     !=     -1)     {   
15                                       bytesum     +=     byteread;       
16                                       System.out.println(bytesum);   
17                                       fs.write(buffer,     0,     byteread);   
18                               }   
19                               inStream.close();   
20                       }   
21               }   
22               catch     (Exception     e)     {   
23                       System.out.println( "error  ");   
24                       e.printStackTrace();   
25               }   
26         }    
27        
28        
29     public static void main(String argv[]){
30       String oldfile = "C:\aa.txt";
31       String newPath = "D:\bb.txt";
32      Copy( oldfile, newPath);
33     }
34 }