在哪里可以找到文件名中允许的字符列表,具体取决于操作系统?(例如在Linux上,字符:在文件名中是允许的,但在Windows上不允许)
我从外部进程收到一个字符串.我想使用该String来创建文件名,然后写入该文件.这是我的代码片段:
String s = ... // comes from external source
File currentFile = new File(System.getProperty("user.home"), s);
PrintWriter currentWriter = new PrintWriter(currentFile);
Run Code Online (Sandbox Code Playgroud)
如果s包含无效字符,例如基于Unix的OS中的"/",则会(正确地)抛出java.io.FileNotFoundException.
如何安全地编码String以便它可以用作文件名?
编辑:我希望的是一个API调用,它为我做这个.
我可以做这个:
String s = ... // comes from external source
File currentFile = new File(System.getProperty("user.home"), URLEncoder.encode(s, "UTF-8"));
PrintWriter currentWriter = new PrintWriter(currentFile);
Run Code Online (Sandbox Code Playgroud)
但我不确定URLEncoder是否可靠用于此目的.
我想从一些随机的Unicode字符串(mich可能只包含任何东西)创建一个健全/安全的文件名(即有些可读,没有"奇怪"字符等).
(对我来说无关紧要,函数是Cocoa,ObjC,Python等)
当然,可能会有无数的字符可能很奇怪.因此,拥有黑名单并在一段时间内向该列表添加越来越多的内容并不是真正的解决方案.
我可以有一个白名单.但是,我真的不知道如何定义它.[a-zA-Z0-9 .]是一个开始,但我也想接受可以正常方式显示的unicode字符.
我不能mkdir用来创建UTF-8字符的文件夹:
<?php
$dir_name = "Depósito";
mkdir($dir_name);
?>
Run Code Online (Sandbox Code Playgroud)
当我在Windows资源管理器中浏览此文件夹时,文件夹名称如下所示:
Depósito
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
我正在使用php5