文件(夹)名避免使用的字符 Characters to Avoid in Directories and Filenames

Characters to Avoid in Directories and Filenames

Your web files will be viewed by numerous users who use a wide variety of operating systems (Mac, PC, and Linux for instance) and devices (desktops, tablets, and smartphones are some examples). Therefore, it is essential to play it safe and avoid common illegal directory and filename characters.

Naming conventions are important in web folders as well as for downloadable files such as PDFS, Word documents, and Excel spreadsheets.

Do not use any of these common illegal characters/symbols:

# pound< left angle bracket$ dollar sign+ plus sign
% percent> right angle bracket! exclamation point` backtick
& ampersand* asterisk‘ single quotes| pipe
{ left bracket? question mark“ double quotes= equal sign
} right bracket/ forward slash: colon 
\ back slashblank spaces@ at sign 

Also, keep these rules in mind.

  • Don’t start or end your filename with a space, period, hyphen, or underline.
  • Keep your filenames to a reasonable length and be sure they are under 31 characters.
  • Most operating systems are case sensitive; always use lowercase.
  • Avoid using spaces and underscores; use a hyphen instead.



what-characters-are-safe-in-cross-platform-file-names-for-linux-windows-and-os

Windows: anything except ASCII's control characters and \/:*?"<>|
Linux, OS-X: anything except null or /

On all platforms it is best to avoid non-printable characters such as the ASCII control-characters.

Windows

In Windows, Windows Explorer does not allow control-characters or \/:*?"<>| You can use spaces. If you use spaces, you will often have to quote the filename when used from the command line (but GUI apps are unaffected so far as I know). Windows filesystem such as NTFS apparently store the encoding with the filename, but UTF-16 is standard.

Linux, OS-X

In Linux and OS-X only / of the printable ASCII set is prohibited I believe. Some characters (shell metacharacters like*?!) will cause problems in command lines and will require the filename to be appropriately quoted or escaped.

Linux filesystems such as ext2, ext3 are character-set agnostic (I think they just treat it more or less as a byte stream - only nulls and/ are prohibited). This means you can store filenames in UTF-8 encoding. I believe it is up to the shell or other application to know what encoding to use to properly convert the filename for display or processing.

Conclusion

So you could probably safely use something like (if it weren't so hard to type)



过滤windows文件名非法字符
windows现在已知的文件名非法字符有 \ / : * ? " < > |

	private static Pattern FilePattern = Pattern.compile("[\\\\/:*?\"<>|]");
	public static String filenameFilter(String str) {
		return str==null?null:FilePattern.matcher(str).replaceAll("");
	}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值