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("复制结束");
}
}