·记录java字符串分割的一个小bug以及解决方法

java中string的split方法使用出现一个bug,具体如下

最后附赠一个简单的程序复制方法(基于java),可以递归的将想要的文件复制到指定的文件件中,并且实现自动修改名字的功能(有疑问可以留言交流)


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileCopy2 {
	void fiel_copy(){
		//文件源
		String addIn = "D:\\matlab";
		//目的地
		String addOut =	"D:\\目的地";	
		//如果觉得类型不够用可以自己修改源码,添加筛选条件
		String type = ".mp4";
		
		//解决方法
		String[] strings = addIn.replace("\\", "-").split("-");
		int sub = addIn.length() - strings[strings.length-1].length();
		//System.out.println(addIn.substring(sub));
		File in = new File(addIn);
		File out = new File(addOut);
		BufferedInputStream ins = null;
		BufferedOutputStream outs = null;
		if(!out.exists()){out.mkdirs();}
		try {
			//ins = new BufferedInputStream(new FileInputStream(in));
			//outs = new BufferedOutputStream(new FileOutputStream(out));
			file_move(in,out,ins,outs,sub,type);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//System.out.println("not exist file");
		}
		
		
	}
	private void file_move(File in, File out, BufferedInputStream ins, BufferedOutputStream outs, int sub, String type) throws Exception {
		// TODO Auto-generated method stub
		System.out.println(in.getPath());
		File[] lif = in.listFiles();
		for (File f : lif) {
			if(f.isDirectory()){
				in = new File(new StringBuilder().append(f.getPath()).toString());
				file_move(in,out,ins,outs, sub, type);
			}
			else{
				String name = f.getName();
				//if (name.endsWith(".m")||name.endsWith(".M")||name.endsWith(".png")) {
				if (name.endsWith(type)) {
					String pname = f.getPath();
					pname = pname.substring(sub);
					pname = pname.replace("\\", "-" );
					//File g = f;
					//boolean to = f.renameTo(new File(pname));
					
					ins = new BufferedInputStream(new FileInputStream(f));
					File file = new File(new StringBuilder().append(out.getPath()).append("\\").append(pname).toString());
					outs = new BufferedOutputStream(new FileOutputStream(file));
					int r;
					while((r=ins.read())!=-1){
						//System.out.print(r);
						outs.write(r);
						//outs.flush();
					}
					//boolean to = f.renameTo(new File(pname));
					ins.close();
					outs.close();
					System.out.println(pname);
					System.out.println("---------------");
				}
				
			}
			
		}
		//System.out.println("---------------");

		
		
	}
	public static void main(String[] args) {
		
		new FileCopy2().fiel_copy();
		System.out.println("复制结束");
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值