public static char[] NON_FILENAME={'\\','/',':','*','?','<','>','|'};
and sample codes.
/*
* Created on 2005/03/31
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.rcp.speech.wizards;
import java.util.HashMap;
import java.io.IOException;
/**
*
*
*/
public class FileNameUtils {
public static char[] NON_FILENAME={'\\','/',':','*','?','<','>','|'};
public static String replaceNonFileName(String base,String replaceText,int maxlength){
StringBuffer result=new StringBuffer();
for(int i=0;i
result.append(replaceText);
}else{
result.append(base.charAt(i));
}
}
return result.toString();
}
public static boolean isNonFileNameChar(char ch){
for (int i = 0; i < NON_FILENAME.length; i++) {
if(NON_FILENAME[i]==ch){
return true;
}
}
return false;
}
public static String[] avoidSameName(String filenames[],String[] otherNames) throws IOException{
int max=999999;
String result[]=new String[filenames.length];
HashMap map=new HashMap();
if(otherNames!=null){
for (int i = 0; i < otherNames.length; i++) {
map.put(otherNames[i],"");
}
}
for (int i = 0; i < result.length; i++) {
String newname=filenames[i];
int last=filenames[i].lastIndexOf(".");
String head=null;
String foot=null;
if(last!=-1){
head=filenames[i].substring(0,last);
foot=filenames[i].substring(last);
}else{
head=filenames[i];
foot="";
}
int sx=1;
while(map.get(newname)!=null){
newname=head+"_"+sx+foot;
sx++;
if(sx>max){
throw new IOException("can't filename renamed");
}
}
result[i]=newname;
map.put(newname,"");
}
return result;
}
}
No comments:
Post a Comment