Friday, June 3, 2011

How can I use Java to change an invalid file name to a valid file name?

I have some files that are picked up by an interface that contains single quotes in the file name: example My_'filestuff'_craz.txt



Not all files coming in have single quotes in them and it is unpredictable when a file with single quotes will be sent to the interface.





I need to remove the single quotes before the file can be journaled.



How can I use JAVA to look at the file name and if the file contains single quotes, take them out. so the file above would be :My_filestuff_craz.txtHow can I use Java to change an invalid file name to a valid file name?I'm sure you could use some slick regular expression to remove it, but that's not my area, so I'll stick with what I know.



Since you only need to remove one character you could use StringTokenizer to tokenize the String at every single quote and then just sum the tokens back up



public String removeQuotes(String str)

{

StringBuffer ret = new StringBuffer();

StringTokenizer st = new StringTokenizer(str, %26quot;'%26quot;);

while(st.hasMoreTokens())

{

ret.append(st.nextToken());

}

return ret.toString();

}



**You may need to escape the single quote using \ in the StringTokenizer statment. Not sure on that one off hand.
  • win32 fujacks ad virus
  • bird
  • No comments:

    Post a Comment