java复制和上传_java文件上传复制等功能
package com.sitech.message.controller.task;
import java.io.File;//引入类
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
//实现文件的简单处理,复制和移动文件、目录等
public class TextCopyFileAndMove {
public static void fileMove(String from, String to) throws Exception {// 移动指定文件夹内的全部文件
try {
File dir = new File(from);
File[] files = dir.listFiles();// 将文件或文件夹放入文件集
if (files == null)// 判断文件集是否为空
return;
File moveDir = new File(to);// 创建目标目录
if (!moveDir.exists()) {// 判断目标目录是否存在
moveDir.mkdirs();// 不存在则创建
}
for (int i = 0; i < files.length; i++) {// 遍历文件集
if (files[i].isDirectory()) {// 如果是文件夹或目录,则递归调用fileMove方法,直到获得目录下的文件
fileMove(files[i].getPath(), to + "\\" + files[i].getName());// 递归移动文件
files[i].delete();// 删除文件所在原目录
}
File moveFile = new File(moveDir.getPath() + "\\"// 将文件目录放入移动后的目录
+ files[i].getName());
if (moveFile.exists()) {// 目标文件夹下存在的话,删除
moveFile.delete();
}
files[i].renameTo(moveFile);// 移动文件
System.out.println(files[i] + " 移动成功");
}
} catch (Exception e) {
throw e;
}
}
// 复制目录下的文件(不包括该目录)到指定目录,会连同子目录一起复制过去。
public static void copyFileFromDir(String toPath, String fromPath) {
File file = new File(fromPath);
createFile(toPath, false);// true:创建文件 false创建目录
if (file.isDirectory()) {// 如果是目录
copyFileToDir(toPath, listFile(file));
}
}
// 复制目录到指定目录,将目录以及目录下的文件和子目录全部复制到目标目录
public static void copyDir(String toPath, String fromPath) {
File targetFile = new File(toPath);// 创建文件
createFile(targetFile, false);// 创建目录
File file = new File(fromPath);// 创建文件
if (targetFile.isDirectory() && file.isDirectory()) {// 如果传入是目录
copyFileToDir(targetFile.getAbsolutePath() + "/" + file.getName(),
listFile(file));// 复制文件到指定目录
}
}
// 复制一组文件到指定目录。targetDir是目标目录,filePath是需要复制的文件路径
public static void copyFileToDir(String toDir, String[] filePath) {
if (toDir == null || "".equals(toDir)) {// 目录路径为空
System.out.println("参数错误,目标路径不能为空");
return;
}
File targetFile = new File(toDir);
if (!targetFile.exists()) {// 如果指定目录不存在
targetFile.mkdir();// 新建目录
} else {
if (!targetFile.isDirectory()) {// 如果不是目录
System.out.println("参数错误,目标路径指向的不是一个目录!");
return;
}
}
for (int i = 0; i < filePath.length; i++) {// 遍历需要复制的文件路径
File file = new File(filePath[i]);// 创建文件
if (file.isDirectory()) {// 判断是否是目录
copyFileToDir(toDir + "/" + file.getName(), listFile(file));// 递归调用方法获得目录下的文件
System.out.println("复制文件 " + file);
} else {
copyFileToDir(toDir, file, "");// 复制文件到指定目录
}
}
}
public static void copyFileToDir(String toDir, File file, String newName) {// 复制文件到指定目录
String newFile = "";
if (newName != null && !"".equals(newName)) {
newFile = toDir + "/" + newName;
} else {
newFile = toDir + "/" + file.getName();
}
File tFile = new File(newFile);
copyFile(tFile, file);// 调用方法复制文件
}
public static void copyFile(File toFile, File fromFile) {// 复制文件
if (toFile.exists()) {// 判断目标目录中文件是否存在
System.out.println("文件" + toFile.getAbsolutePath() + "已经存在,跳过该文件!");
return;
} else {
createFile(toFile, true);// 创建文件
}
System.out.println("复制文件" + fromFile.getAbsolutePath() + "到"
+ toFile.getAbsolutePath());
try {
InputStream is = new FileInputStream(fromFile);// 创建文件输入流
FileOutputStream fos = new FileOutputStream(toFile);// 文件输出流
byte[] buffer = new byte[1024];// 字节数组
while (is.read(buffer) != -1) {// 将文件内容写到文件中
fos.write(buffer);
}
is.close();// 输入流关闭
fos.close();// 输出流关闭
} catch (FileNotFoundException e) {// 捕获文件不存在异常
e.printStackTrace();
} catch (IOException e) {// 捕获异常
e.printStackTrace();
}
}
public static String[] listFile(File dir) {// 获取文件绝对路径
String absolutPath = dir.getAbsolutePath();// 声获字符串赋值为路传入文件的路径
String[] paths = dir.list();// 文件名数组
String[] files = new String[paths.length];// 声明字符串数组,长度为传入文件的个数
for (int i = 0; i < paths.length; i++) {// 遍历显示文件绝对路径
files[i] = absolutPath + "/" + paths[i];
}
return files;
}
public static void createFile(String path, boolean isFile) {// 创建文件或目录
createFile(new File(path), isFile);// 调用方法创建新文件或目录
}
public static void createFile(File file, boolean isFile) {// 创建文件
if (!file.exists()) {// 如果文件不存在
if (!file.getParentFile().exists()) {// 如果文件父目录不存在
createFile(file.getParentFile(), false);
} else {// 存在文件父目录
if (isFile) {// 创建文件
try {
file.createNewFile();// 创建新文件
} catch (IOException e) {
e.printStackTrace();
}
} else {
file.mkdir();// 创建目录
}
}
}
}
public static void main(String[] args) {// java程序主入口处
String fromPath = "E:/createFile/shafei.log";// 目录路径
String toPath = "F:/createFile/shafei.log";// 源路径
System.out.println("1.移动文件:从路径 " + fromPath + " 移动到路径 " + toPath);
File fromPaths = new File("F:/createFile/createFile/test.log");
File tofiles = new File("F:/createFile/shafei/test.log");
copyFile(tofiles,fromPaths);
try {
fileMove(fromPath, toPath);// 调用方法实现文件的移动
} catch (Exception e) {
System.out.println("移动文件出现问题" + e.getMessage());
}
System.out.println("2.复制目录 " + toPath + " 下的文件(不包括该目录)到指定目录" + fromPath
+ " ,会连同子目录一起复制过去。");
copyFileFromDir(fromPath, toPath);// 调用方法实现目录复制
System.out.println("3.复制目录 " + fromPath + "到指定目录 " + toPath
+ " ,将目录以及目录下的文件和子目录全部复制到目标目录");
copyDir(toPath, fromPath);// 调用方法实现目录以用目录下的文件和子目录全部复制
}
}
java复制和上传_java文件上传复制等功能相关推荐
- java文件上传_Java文件上传细讲
什么是文件上传? 文件上传就是把用户的信息保存起来. 为什么需要文件上传? 在用户注册的时候,可能需要用户提交照片.那么这张照片就应该要进行保存. 上传组件(工具) 为什么我们要使用上传工具? 为啥我 ...
- java 调用 swf 文件上传_java文件上传方法
文件上传方法(一次上传一个文件,多个文件的话,请写循环调用) Upload.uploadFile(theFile, filePath) 说明: theFile:类型是FormFile filePath ...
- java 连接 sftp失败_java – 文件上传到SFTP失败(Apache VFS)
我有一个SFTP问题( Windows WinSSHD).我尝试用 Apache Commons VFS在一个文件夹中写一个文件.在本地SFTP上我没有上传的问题,但在第二个SFTP上我总是得到以下错 ...
- java 图片上传_java web图片上传和文件上传实例
本篇文章主要介绍了java web图片上传和文件上传实例,具有一定的参考价值,有需要的可以了解一下. 图片上传和文件上传本质上是一样的,图片本身也是文件.文件上传就是将图片上传到服务器,方式虽然有很多 ...
- java web文件上传详解_java web图片上传和文件上传实例详解
java web图片上传和文件上传 图片上传和文件上传本质上是一样的,图片本身也是文件.文件上传就是将图片上传到服务器,方式虽然有很多,但底层的实现都是文件的读写操作. 注意事项 1.form表单一定 ...
- java上传下载_Java文件上传与文件下载实现方法详解
本文实例讲述了Java文件上传与文件下载实现方法.分享给大家供大家参考,具体如下: Java文件上传 数据上传是客户端向服务器端上传数据,客户端向服务器发送的所有请求都属于数据上传.文件上传是数据上传 ...
- java使用smartupload组件实现文件上传的方法
为什么80%的码农都做不了架构师?>>> 本文实例讲述了java使用smartupload组件实现文件上传的方法.分享给大家供大家参考.具体分析如下:文件上传几乎是所有网站都具 ...
- Java实现FTP批量大文件上传下载
用Java实现FTP批量大文件上传下载 <iframe id="I0_1416224567509" style="margin: 0px; padding: 0px ...
- java上传文件到Oracle_用java实现ORACLE数据库的文件上传、下载
文件上传使用: 的 UploadBean JAVA包,他的文件上传做的还不错,可进行目录或数据库的文件上传,默认使用的是ORACLE 的LONGRAW子段类型,可使用BLOB类型,但需要使用扩展JAV ...
最新文章
- r语言 图形一览_R语言之图形概览
- zabbix企业应用之bind dns监控(转)
- swift php 类型判断,iOS开发 - Swift实现检测网络连接状态及网络类型
- bullet HashMap 内存紧密的哈希表
- 解决Linux下vi或vim操作Found a swap file by the name
- 信息学奥赛一本通(1197:山区建小学)
- 信息系统项目管理通关指南
- anroid Sqlite批量插入数据优化方法
- document.all用法
- 十六、oracle 索引
- 数学建模之向量自回归模型
- 医学流体力学血流动力学仿真模拟计算及临床应用
- chmod 755 与chmod +x的区别
- android+串口+键盘,串口转USB HID键盘鼠标方案
- 【白帽子学习笔记】CTF实践
- 计算机三种校验方式,三种校验码
- matlab矩阵逆时针旋转90度
- hosts文件详解--localhost和127.0.0.1
- 分享一个latex的beamer模板
- unity2d游戏开发系列教程:一、环境安装
热门文章
- java transform的作用_为什么旋转(0deg)很重要?
- 使C#代码现代化——第四部分:类型
- 关于thymeleaf配置语法运用 以及 静态资源问题总结 2021-06-08
- linux远程连接 ipv6,Ansible 配置 IPv6 连接
- io.jsonwebtoken.ExpiredJwtException jwt令牌过期
- oracle在指定列后添加列,oracle添加列到指定位置
- php分解字符串_php怎么把字符串分解成字符
- c语言剪枝函数,[求助]关于剪枝算法
- python设计模式案例分析_Python设计模式之职责链模式原理与用法实例分析
- linux找回删除的文件6,在Centos6/RHEL6上恢复ext4文件系统下误删除的文件