java 复制 删除 改文件 后缀名小程序
2015-12-22 00:00
211 查看
public class FindandCopy {
//查找文件
public static void findmethod(File file1,File file2,String findtype,String changetype) throws Exception{
if(!file1.exists()){
return ;
}else if(file1.isFile()){
//文件夹不存在就创建
if(!file2.exists()){
file2.mkdirs();
}
if(file1.getName().endsWith(findtype)){
copy(file1,file2,findtype,changetype);
}
}else if(file1.isDirectory()){
File[] files = file1.listFiles();
for(File f : files){
findmethod(f,new File(file2 + "\\" + file1.getName()),findtype,changetype);
}
}
}
//复制文件
public static void copy(File pathr,File pathw,String findtype,String changetype) throws Exception{
InputStream is = new FileInputStream(pathr);
OutputStream os = new FileOutputStream(pathw + "\\" + pathr.getName().replaceAll(findtype,changetype));
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os);
//读 写 文件
byte[] b = new byte[1024];
int length = 0;
while((length = bis.read(b)) != -1){
bos.write(b,0,length);
bos.flush();
// bis.close();
// bos.close();
}
}
//删除
public static void deletefile(File file){
if(!file.exists()){
return ;
}else if(file.isFile()){
file.delete();
}else if(file.isDirectory()){
File[] files = file.listFiles();
for(File f : files){
deletefile(f);
f.delete();
//if(f.exists()){f.delete();}
}
file.delete();
}
}
}
------------
Test 类
public class Test extends FindandCopy{
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("------------------复制与删除------------------\n");
System.out.println("\t\t[1] 复制\n\t\t[2] 删除\n\t\t[0] 退出\n请选择操作:");
while(true){
int chose = sc.nextInt();
switch(chose){
case 1 :
System.out.println("请输入要复制的位置(路径)");
File f1 = new File(sc.next());
System.out.println("请输入要拷贝到的位置(路径)");
File f2 = new File(sc.next());
System.out.println("请输入要复制前的文件后缀名");
String findtype = sc.next();
System.out.println("请输入要拷贝后的文件后缀名");
String changetype = sc.next();
findmethod(f1,f2,findtype,changetype);
//打印出当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String time = sdf.format(new Date());
System.out.println(time);
break;
case 2 :
System.out.println("请输入要删除的位置(路径)");
File deletefind = new File(sc.next());
deletefile(deletefind);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String time1 = sdf1.format(new Date());
System.out.println(time1);
break;
case 0 :System.exit(0);
default:
}
}
}
}
//查找文件
public static void findmethod(File file1,File file2,String findtype,String changetype) throws Exception{
if(!file1.exists()){
return ;
}else if(file1.isFile()){
//文件夹不存在就创建
if(!file2.exists()){
file2.mkdirs();
}
if(file1.getName().endsWith(findtype)){
copy(file1,file2,findtype,changetype);
}
}else if(file1.isDirectory()){
File[] files = file1.listFiles();
for(File f : files){
findmethod(f,new File(file2 + "\\" + file1.getName()),findtype,changetype);
}
}
}
//复制文件
public static void copy(File pathr,File pathw,String findtype,String changetype) throws Exception{
InputStream is = new FileInputStream(pathr);
OutputStream os = new FileOutputStream(pathw + "\\" + pathr.getName().replaceAll(findtype,changetype));
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os);
//读 写 文件
byte[] b = new byte[1024];
int length = 0;
while((length = bis.read(b)) != -1){
bos.write(b,0,length);
bos.flush();
// bis.close();
// bos.close();
}
}
//删除
public static void deletefile(File file){
if(!file.exists()){
return ;
}else if(file.isFile()){
file.delete();
}else if(file.isDirectory()){
File[] files = file.listFiles();
for(File f : files){
deletefile(f);
f.delete();
//if(f.exists()){f.delete();}
}
file.delete();
}
}
}
------------
Test 类
public class Test extends FindandCopy{
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("------------------复制与删除------------------\n");
System.out.println("\t\t[1] 复制\n\t\t[2] 删除\n\t\t[0] 退出\n请选择操作:");
while(true){
int chose = sc.nextInt();
switch(chose){
case 1 :
System.out.println("请输入要复制的位置(路径)");
File f1 = new File(sc.next());
System.out.println("请输入要拷贝到的位置(路径)");
File f2 = new File(sc.next());
System.out.println("请输入要复制前的文件后缀名");
String findtype = sc.next();
System.out.println("请输入要拷贝后的文件后缀名");
String changetype = sc.next();
findmethod(f1,f2,findtype,changetype);
//打印出当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String time = sdf.format(new Date());
System.out.println(time);
break;
case 2 :
System.out.println("请输入要删除的位置(路径)");
File deletefind = new File(sc.next());
deletefile(deletefind);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String time1 = sdf1.format(new Date());
System.out.println(time1);
break;
case 0 :System.exit(0);
default:
}
}
}
}
相关文章推荐
- 微信生态圈盈利模式分析
- android中service的总结,特别是关于如何设置类似qq,微信防止被清除
- Android 仿微信支付密码输入框
- 微信公众号antian365sec
- iOS之微信支付开发
- Android应用实现微信登录与分享
- 微信支付测试OK。
- 微信相关 -- 可以通过调用下面的方法 请求对应的url 获取微信服务器返回的json数据
- PHP微信SDK——Zebra-Wechat
- 关于微信编辑器,我有话说~~~
- 关于php微信订阅号开发之token验证后自动发送消息给订阅号但是没有消息返回的问题
- 微信支付-小小工程狮的成长之路
- 微信app支付php开发
- 微信支付与支付宝钱包的竞争分析
- 自己的服务器通过微信公众号Token验证测试的代码(Python版)
- android中关于图片分享到QQ或者微信,朋友圈及其他(免SDK)
- 我们为什么这么拼?(转载自微信)
- 部署时System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
- php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)
- java实现微信支付(服务端)