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 }
复制代码

 





本文转自夏雪冬日博客园博客,原文链接:http://www.cnblogs.com/heyonggang/p/3158239.html,如需转载请自行联系原作者

目录
相关文章
|
2月前
|
Java Unix Go
【Java】(8)Stream流、文件File相关操作,IO的含义与运用
Java 为 I/O 提供了强大的而灵活的支持,使其更广泛地应用到文件传输和网络编程中。!但本节讲述最基本的和流与 I/O 相关的功能。我们将通过一个个例子来学习这些功能。
180 1
|
5月前
|
存储 Java 编译器
深入理解Java虚拟机--类文件结构
本内容介绍了Java虚拟机与Class文件的关系及其内部结构。Class文件是一种与语言无关的二进制格式,包含JVM指令集、符号表等信息。无论使用何种语言,只要能生成符合规范的Class文件,即可在JVM上运行。文章详细解析了Class文件的组成,包括魔数、版本号、常量池、访问标志、类索引、字段表、方法表和属性表等,并说明其在Java编译与运行过程中的作用。
139 0
|
5月前
|
存储 人工智能 Java
java之通过Http下载文件
本文介绍了使用Java实现通过文件链接下载文件到本地的方法,主要涉及URL、HttpURLConnection及输入输出流的操作。
314 0
|
5月前
|
监控 Java API
Java语言按文件创建日期排序及获取最新文件的技术
这段代码实现了文件创建时间的读取、文件列表的获取与排序以及获取最新文件的需求。它具备良好的效率和可读性,对于绝大多数处理文件属性相关的需求来说足够健壮。在实际应用中,根据具体情况,可能还需要进一步处理如访问权限不足、文件系统不支持某些属性等边界情况。
257 14
|
6月前
|
存储 Java 数据安全/隐私保护
Java技术栈揭秘:Base64加密和解密文件的实战案例
以上就是我们今天关于Java实现Base64编码和解码的实战案例介绍。希望能对你有所帮助。还有更多知识等待你去探索和学习,让我们一同努力,继续前行!
468 5
|
6月前
|
网络协议 安全 Java
实现Java语言的文件断点续传功能的技术方案。
像这样,我们就完成了一项看似高科技、实则亲民的小工程。这样的技术实现不仅具备实用性,也能在面对网络不稳定的挑战时,稳稳地、不失乐趣地完成工作。
346 0
|
9月前
|
前端开发 Cloud Native Java
Java||Springboot读取本地目录的文件和文件结构,读取服务器文档目录数据供前端渲染的API实现
博客不应该只有代码和解决方案,重点应该在于给出解决方案的同时分享思维模式,只有思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~
Java||Springboot读取本地目录的文件和文件结构,读取服务器文档目录数据供前端渲染的API实现
|
Java
JAVA读取文件的几种方法
喜欢的朋友可以关注下,粉丝也缺。 InputStreamReader+BufferedReader读取字符串 InputStreamReader 将字节流转换为字符流。
1382 0
[Java]读取文件方法大全
1、按字节读取文件内容2、按字符读取文件内容3、按行读取文件内容 4、随机读取文件内容 public class ReadFromFile {    /**     * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
797 0