- 浏览: 16337 次
- 性别:
- 来自: 武汉
文章分类
最新评论
package com.wepull.demo;
import java.io.*;
import java.util.zip.*;
/** *//**
* 对文件或者目录操作的类
* @version 1.0
* @author leno
*/
public class FileUtil {
private static void copy(File source, File target) throws IOException {
File tar = new File(target, source.getName());
if (source.isDirectory()) {
System.out.println("开始创建目录:" + tar.getPath());
tar.mkdir();
File[] fs = source.listFiles();
for (int i = 0; i < fs.length; i++) {
copy(fs[i], tar);
}
} else {
System.out.println("开始从" + source + "拷贝文件到" + tar.getPath());
InputStream is = new FileInputStream(source);
OutputStream os = new FileOutputStream(tar);
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1) {
os.write(buf, 0, len);
}
is.close();
os.close();
}
}
/** *//**
* 拷贝文件或者目录到某个指定的路径
*
* @param source
* 源文件或者目录
* @param target
* 目标路径
* @throws IOException
*/
public static void copy(String source, String target) {
File sour = new File(source);
File tar = new File(target);
try {
copy(sour, tar);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void delete(File file) {
if (file.isDirectory()) {
File[] fs = file.listFiles();
for (int i = 0; i < fs.length; i++) {
delete(fs[i]);
}
file.delete();
} else {
file.delete();
}
}
/** *//**
* 删除一个文件或者目录
*
* @param file
*/
public static void delete(String path) {
File file = new File(path);
delete(file);
}
/** *//**
* 压缩文件或者目录到指定的路径
*
* @param zipFileName
* 目标路径
* @param inputPath
* 被压缩的文件或者目录
*/
public static void zip(String zipFileName, String inputPath) {
File inputFile = new File(inputPath);
ZipOutputStream out;
try {
out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, inputFile, inputFile.getName());
System.out.println("压缩完成!");
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void zip(ZipOutputStream out, File f, String base)
throws Exception {
System.out.println("正在压缩:" + f.getName() + " ");
if (f.isDirectory()) {
File[] fs = f.listFiles();
base += "/";
System.out.println("新建目录条目:" + f.getName());
out.putNextEntry(new ZipEntry(base)); // 生成相应的目录
for (int i = 0; i < fs.length; i++) {
// 对本目录下的所有文件对象递归调用本方法
zip(out, fs[i], base + fs[i].getName());
}
} else {
System.out.println("新增文件条目:" + f.getName());
out.putNextEntry(new ZipEntry(base));
InputStream is = new FileInputStream(f);
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1) {
out.write(buf, 0, len);
}
is.close();
}
}
/** *//**
* 解压缩zip文件到指定的路径
*
* @param zipfile
* zip格式压缩文件
* @param desPath
* 目标路径
*/
public static void unzip(String zipFile, String desPath) {
// 建立输出流,用于将从压缩文件中读出的文件流写入到磁盘
OutputStream out = null;
// 建立输入流,用于从压缩文件中读出文件
ZipInputStream is;
try {
is = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry entry = null;
while ((entry = is.getNextEntry()) != null) {
System.out.println("正在解压缩:" + entry.getName() + " ");
File f = new File(desPath + "\\" + entry.getName());
if (entry.isDirectory()) {
System.out.println("新建目录:" + f.getName());
f.mkdir();
} else {
System.out.println("新增文件:" + f.getName());
// 根据压缩文件中读出的文件名称新建文件
out = new FileOutputStream(f);
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.close();
}
}
is.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** *//**
* 创建新文件
*
* @param path
*/
public static void createFile(String path) {
File file = new File(path);
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** *//**
* 创建新目录
*
* @param path
*/
public static void createDir(String path) {
File file = new File(path);
file.mkdirs();
}
/** *//**
* 剪切文件或者目录到某个指定的路径
*
* @param source
* 源文件或者目录
* @param target
* 目标路径
*
*/
public static void cutTo(String source, String target) {
File sourFile = new File(source);
File tarFile = new File(target);
if (sourFile.isFile()) {
if (tarFile.isDirectory()) {
sourFile.renameTo(tarFile);
}
} else {
copy(source, target);
delete(source);
}
}
public static void main(String[] args) {
// copy("E:\\w.txt", "E:\\a");
// delete("E:\\a");
// zip("E:\\a.zip", "E:\\b");
// unzip("E:\\a.zip", "E:\\b");
// createFile("E:\\a.txt");
// createDir("E:\\bb");
// cutTo("E:\\b", "D:\\");
}
}
import java.io.*;
import java.util.zip.*;
/** *//**
* 对文件或者目录操作的类
* @version 1.0
* @author leno
*/
public class FileUtil {
private static void copy(File source, File target) throws IOException {
File tar = new File(target, source.getName());
if (source.isDirectory()) {
System.out.println("开始创建目录:" + tar.getPath());
tar.mkdir();
File[] fs = source.listFiles();
for (int i = 0; i < fs.length; i++) {
copy(fs[i], tar);
}
} else {
System.out.println("开始从" + source + "拷贝文件到" + tar.getPath());
InputStream is = new FileInputStream(source);
OutputStream os = new FileOutputStream(tar);
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1) {
os.write(buf, 0, len);
}
is.close();
os.close();
}
}
/** *//**
* 拷贝文件或者目录到某个指定的路径
*
* @param source
* 源文件或者目录
* @param target
* 目标路径
* @throws IOException
*/
public static void copy(String source, String target) {
File sour = new File(source);
File tar = new File(target);
try {
copy(sour, tar);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void delete(File file) {
if (file.isDirectory()) {
File[] fs = file.listFiles();
for (int i = 0; i < fs.length; i++) {
delete(fs[i]);
}
file.delete();
} else {
file.delete();
}
}
/** *//**
* 删除一个文件或者目录
*
* @param file
*/
public static void delete(String path) {
File file = new File(path);
delete(file);
}
/** *//**
* 压缩文件或者目录到指定的路径
*
* @param zipFileName
* 目标路径
* @param inputPath
* 被压缩的文件或者目录
*/
public static void zip(String zipFileName, String inputPath) {
File inputFile = new File(inputPath);
ZipOutputStream out;
try {
out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, inputFile, inputFile.getName());
System.out.println("压缩完成!");
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void zip(ZipOutputStream out, File f, String base)
throws Exception {
System.out.println("正在压缩:" + f.getName() + " ");
if (f.isDirectory()) {
File[] fs = f.listFiles();
base += "/";
System.out.println("新建目录条目:" + f.getName());
out.putNextEntry(new ZipEntry(base)); // 生成相应的目录
for (int i = 0; i < fs.length; i++) {
// 对本目录下的所有文件对象递归调用本方法
zip(out, fs[i], base + fs[i].getName());
}
} else {
System.out.println("新增文件条目:" + f.getName());
out.putNextEntry(new ZipEntry(base));
InputStream is = new FileInputStream(f);
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1) {
out.write(buf, 0, len);
}
is.close();
}
}
/** *//**
* 解压缩zip文件到指定的路径
*
* @param zipfile
* zip格式压缩文件
* @param desPath
* 目标路径
*/
public static void unzip(String zipFile, String desPath) {
// 建立输出流,用于将从压缩文件中读出的文件流写入到磁盘
OutputStream out = null;
// 建立输入流,用于从压缩文件中读出文件
ZipInputStream is;
try {
is = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry entry = null;
while ((entry = is.getNextEntry()) != null) {
System.out.println("正在解压缩:" + entry.getName() + " ");
File f = new File(desPath + "\\" + entry.getName());
if (entry.isDirectory()) {
System.out.println("新建目录:" + f.getName());
f.mkdir();
} else {
System.out.println("新增文件:" + f.getName());
// 根据压缩文件中读出的文件名称新建文件
out = new FileOutputStream(f);
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.close();
}
}
is.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** *//**
* 创建新文件
*
* @param path
*/
public static void createFile(String path) {
File file = new File(path);
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** *//**
* 创建新目录
*
* @param path
*/
public static void createDir(String path) {
File file = new File(path);
file.mkdirs();
}
/** *//**
* 剪切文件或者目录到某个指定的路径
*
* @param source
* 源文件或者目录
* @param target
* 目标路径
*
*/
public static void cutTo(String source, String target) {
File sourFile = new File(source);
File tarFile = new File(target);
if (sourFile.isFile()) {
if (tarFile.isDirectory()) {
sourFile.renameTo(tarFile);
}
} else {
copy(source, target);
delete(source);
}
}
public static void main(String[] args) {
// copy("E:\\w.txt", "E:\\a");
// delete("E:\\a");
// zip("E:\\a.zip", "E:\\b");
// unzip("E:\\a.zip", "E:\\b");
// createFile("E:\\a.txt");
// createDir("E:\\bb");
// cutTo("E:\\b", "D:\\");
}
}
发表评论
-
json数据处理
2016-06-22 15:16 458public static <T> T rea ... -
java的同步异步调用
2016-03-06 18:09 856软件模块之间总是存在着一定的接口,从调用方式上,可以把他们分 ... -
shiro初级认识
2016-02-29 18:21 794一、 介绍: shiro是apache提供的强大而灵活 ... -
校验身份证号码及编码规则
2015-12-10 15:52 776身份证前6位【ABCDEF】为行政区划数字代码(简称数字码) ... -
org.apache.commons.httpclient的jar包的使用
2015-01-21 15:15 25761.创建HttpClient实例 HttpClient ... -
checkbox实现全选
2014-12-12 10:54 628<%@ page contentType=" ... -
servlet实现多文件上传
2014-12-12 10:44 5341.fileupload.jsp<%@ page l ... -
servlet实现文件的下载
2014-12-12 09:53 483public class DownLoadServlet ex ... -
jxl对Excel的操作,包含合并单元格
2014-12-11 15:50 159801.import java.io.*; 02.im ... -
实现将数据导入到excel中或者从excel中将数据导入到数据库
2014-10-21 10:21 690/** * 将数据写入Excel中 */ public ...
相关推荐
用java实现的文件和文件夹拷贝和转移
windows下批量拷贝目录下所有文件夹中的...肯定不能改动文件的名字,就是拷贝到一个文件中,方便后期的文件查看、转移和分析,简单的说的话就是把存放数据的那个盘下面的所有文件夹中的variant文件(.vcf)拷贝出来;
Fastcopy是日本的最快的文件拷贝工具。磁盘间相互拷贝文件是司空见惯的事情,通常情况都是利用WINDOWS自带的复制粘贴功能来实现的。这样做在数目较小的情况下倒也没什么问题,但是数目较大时,系统自带的复制功能就...
C++ 文件操作类(拷贝、删除、移动、改名)~
本程序运行于.net 2.0下的绿色软件,以windows服务方式运行,直接更改配置文件,可以运行多个复制作业,每个作业可以指定运行时间、拷贝方式、是否压缩、运行周期等参数,特别是数据库备份,压缩后体积将缩小很多。
vc实现文件夹从一个目录下拷贝到另一个目录源代码
比如想拷贝D盘下123文件夹中A、B、C……等子文件夹下的mp3文件到D盘下的music文件夹中。 大大提高工作效率。
建立文件3,删除文件或者目录(整个目录)4,文件改名5,可同时上传8个文件6,在线文件编辑7,可更改文件属性8,可拷贝整个目录(注意,是拷贝里面的文件,如你选的是images这个目录的话就拷贝里面的文件和目录 ...
因为一些原因,需要换一台VPS,所以就需要为服务器转移文件,下面介绍一下linux使用scp命令转移备份文件的方法
可以将文件夹或者文件进行数据的拷贝与转移。
2.指定一个路径,将其下面(含子文件夹中)的以".txt"结尾的文件移动到指定的另一个路径下;
8、本软件是将文件通过加密算法运算后转换成密文,然后重新写回磁盘,这种真正的加密和那些把文件转移隐藏到一些特殊文件夹,而实际上文件在磁盘上还是明文存储的加密方式有本质区别,真正的加密可以从根本上保障您...
7、本软件是将文件通过加密算法运算后转换成密文,然后重新写回磁盘,这种真正的加密和那些把文件转移隐藏到一些特殊文件夹,而实际上文件在磁盘上还是明文存储的加密方式有本质区别,真正的加密可以从根本上保障您...
1) 手动拷贝要转移的数据数据文件'd:\OracleData\GWTABLE42.DBF'到新的位置'E:\OracleData\GWTABLE42.DBF'
经过KeyID和密码双重认证后才能打开密盘,允许普通用户在密盘里自由工作(创建、删除、编辑、修改、保存任意格式的文件),但未经管理员授权不能以各种方式将密盘里的文件拷贝或转移出去,一旦退出即自动隐藏密盘且...
在机器2上将从机器1服务器上拷贝过来的目录及文件所在的文件夹共享出来,设置好相应的ntfs权限。 b. 在Windows Server上用管理员权限运行CMD后,用下面的命令来复制共享权限 Permcopy //源机器名共享文件夹名 //...
FastCopy 是 Windows 平台上最快的文件拷贝、删除软件,它支持 UNICODE 和超过 MAX_PATH (260 个字符)的文件路径名。由于其功能强劲,性能优越,一时间便超越相同类型的所有其他软件。由于该软件十分小巧,你甚至...
使用c++/c#编写一个console程序,可以模拟dos下...此项目就是编制一个程序,模拟这种功能,并且在内存中建立一个真正的内存磁盘,可以任意的创建、删除目录,拷贝、删除文件,列出目录、文件,转移当前所在路径等功能。
应用程序之所以不能像一般用户文件夹那样随心所欲地转移到其它分区,是因为其中关乎到许多程序运行时所必需的相关信息,如快捷方式、注册表键值和INI文件等,所以在搬迁文件夹的时候,必须根据所迁移的分区,适当地...