文件备份类——copy别人的
来源:互联网 发布:装饰360设计软件 编辑:程序博客网 时间:2024/06/05 20:49
package Uitls;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 用于文件备份的类
*
* 适用于各种类型文件备份,在原文件的路径下,创建备份文件,命名为 原文件名.bak
*/
public class FileUtils {
public static String BACKUP_SUFFIX =".bak";
/**
* 实现文件复制的函数
*
* 采用二进制流的形式来实现文件的读写
*/
public static void fileCopy(File srcFile, File destFile) throws Exception{
//创建文件输入缓冲流
InputStream src = new BufferedInputStream(new FileInputStream(srcFile));
//创建文件输出缓冲流
OutputStream dest = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] trans = new byte[1024];
int count = -1;
while((count = src.read(trans)) != -1){
dest.write(trans, 0, count);
}
dest.flush();
src.close();
dest.close();
}
/**
* 备份文件,在原文件目录下创建备份文件,命名为 原文件名.bak
* @param templateFile 需要备份的函数
* @return true 成功,false 失败
*/
public static boolean backupTemplateFile(String templateFile){
boolean flag = true;
File srcFile = new File(templateFile);
if(!srcFile.exists()){
System.out.println("模板文件不存在");
return false;
}
//创建备份文件
File backUpFile = new File(templateFile+BACKUP_SUFFIX);
try {
if(backUpFile.createNewFile()){
//创建备份文件成功,进行文件复制
fileCopy(srcFile, backUpFile);
}
} catch (Exception e) {
flag = false;
System.out.println("备份文件失败");
}
return flag;
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 用于文件备份的类
*
* 适用于各种类型文件备份,在原文件的路径下,创建备份文件,命名为 原文件名.bak
*/
public class FileUtils {
public static String BACKUP_SUFFIX =".bak";
/**
* 实现文件复制的函数
*
* 采用二进制流的形式来实现文件的读写
*/
public static void fileCopy(File srcFile, File destFile) throws Exception{
//创建文件输入缓冲流
InputStream src = new BufferedInputStream(new FileInputStream(srcFile));
//创建文件输出缓冲流
OutputStream dest = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] trans = new byte[1024];
int count = -1;
while((count = src.read(trans)) != -1){
dest.write(trans, 0, count);
}
dest.flush();
src.close();
dest.close();
}
/**
* 备份文件,在原文件目录下创建备份文件,命名为 原文件名.bak
* @param templateFile 需要备份的函数
* @return true 成功,false 失败
*/
public static boolean backupTemplateFile(String templateFile){
boolean flag = true;
File srcFile = new File(templateFile);
if(!srcFile.exists()){
System.out.println("模板文件不存在");
return false;
}
//创建备份文件
File backUpFile = new File(templateFile+BACKUP_SUFFIX);
try {
if(backUpFile.createNewFile()){
//创建备份文件成功,进行文件复制
fileCopy(srcFile, backUpFile);
}
} catch (Exception e) {
flag = false;
System.out.println("备份文件失败");
}
return flag;
}
}
阅读全文
0 0
- 文件备份类——copy别人的
- 【实例】copy别人的网页
- poi读取exl(copy别人的)
- 误删别人的文件,备份软件很重要,自己想写一个
- VB自定义文件COPY的类.
- Copy文件的顺序
- Copy文件的方法
- 超大文件的copy
- python_文件的copy
- 程序员的成长过程(copy别人的)
- abstract class与interface的区别(copy别人的)
- Copy文件工具类
- OC基础——copy语法(Student属性的copy、Student对象的copy、Student子类对象的copy)
- 看别人写的一个多文件下载类
- 文件的copy与合并
- Copy文件错误的提示
- 通讯录程序的改进——文件备份
- 总结别人的心得——
- aria ArCommands的用法
- Docker 安装 Nginx
- iOS_CodeSnippets
- JavaScript、jQuery和Ajax初步了解
- Android studio的下载和安装
- 文件备份类——copy别人的
- Spring源码分析3 — spring bean创建和初始化
- No toolchains found in the NDK toolchains folder for ABI with prefix: arm-linux-androideabi
- Spark与Pandas中DataFrame对比
- 【Scikit-Learn 中文文档】机器学习: scikit-learn 中的设置以及预估对象
- linux 僵尸进程解决办法
- 欢迎使用CSDN-markdown编辑器
- Java reflect 反射机制与泛型擦除
- 搭建jdk+tomcat+eclipse开发环境