How can I safely encode a string in Java to use as a filename?

后端 未结 9 2050
失恋的感觉
失恋的感觉 2020-11-29 22:53

I\'m receiving a string from an external process. I want to use that String to make a filename, and then write to that file. Here\'s my code snippet to do this:



        
9条回答
  •  一个人的身影
    2020-11-29 23:21

    Try using the following regex which replaces every invalid file name character with a space:

    public static String toValidFileName(String input)
    {
        return input.replaceAll("[:\\\\/*\"?|<>']", " ");
    }
    

提交回复
热议问题