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 }
- 修改WordPress登陆文件名wp-login.php,防密码被暴力破解
- NDoc - .NET 代码文档生成器
- Mono P/Invoke :DLLImport
- MONO x64 amd_x64
- DN榜:最近3个域名成交金额均达到六位数美金
- 采访Philipp Crocoll:安卓平台上整合Java和C#
- “AS3.0高级动画编程”学习:第一章高级碰撞检测
- AI分析师PK人类分析师,结果竟然是……
- 纯代码修改WordPress 默认 Gravatar 头像的方法
- 这些行业的饭碗要被机器人抢了 再不努力就被它们PK掉了!
- DeveWork.com 上线“前端工具箱”(附源码下载)
- Debug和Release之本质区别
- ArgoUML -- 开源UML 建模工具
- 性能分析工具-PerfView
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- HDU 1421 搬寝室 解题报告(超详细)
- codeforce 270B Multithreading
- flex-wrap align-content详解
- Code force-CodeCraft-20 (Div. 2) D. Nash Matrix 详解(DFS构造)
- HDU Problem D [ Humble number ]——基础DP丑数序列
- Java开发编程规范: 3.代码格式
- flex-direction
- css中清除浮动方式五
- css中-清除浮动方式四
- forin forof forEach myForEach
- 深拷贝,你懂吗?
- Codeforce-CodeCraft-20 (Div. 2)-C. Primitive Primes(本原多项式+数学推导)
- DOM事件机制(原理级别的)
- 杭电60题--part 1 HDU1003 Max Sum(DP 动态规划)
- js的的的图片随屏幕滚动而滑入滑出的效果(万 万。。。字长文)