使用.properties文件读取Windows字体文件时出现路径语法错误,如何正确使用?
解决.properties文件读取字体路径的FileNotFoundException问题
问题根源
你遇到的错误是因为.properties文件里的路径值被额外添加了双引号。当通过prop.getProperty("verdanaI")获取值时,得到的字符串是"C:\\Windows\\Fonts\\Verdanai.ttf"(包含首尾的双引号),而系统实际需要的路径是不带双引号的C:\Windows\Fonts\Verdanai.ttf,多余的双引号导致路径语法错误,触发了文件找不到的异常。
解决方法
方法1:修正.properties文件内容
直接去掉所有路径值的首尾双引号,修改后的文件内容如下:
verdana=C:\\Windows\\Fonts\\Verdana.ttf verdanaI=C:\\Windows\\Fonts\\Verdanai.ttf verdanaB=C:\\Windows\\Fonts\\Verdanab.ttf arial=C:\\Windows\\Fonts\\arial.ttf arialI=C:\\Windows\\Fonts\\ariali.ttf arialB=C:\\Windows\\Fonts\\arialbd.ttf calibri=C:\\Windows\\Fonts\\calibri.ttf calibriB=C:\\Windows\\Fonts\\calibrib.ttf wingding=C:\\Windows\\Fonts\\wingding.ttf
.properties文件的键值对中,值不需要用双引号包裹,直接写入内容即可,Properties类会正确解析转义后的路径。
方法2:读取后手动去除双引号(临时方案)
如果暂时无法修改.properties文件,可以在代码中获取路径后,手动剔除首尾的双引号:
String verdanaIPath = prop.getProperty("verdanaI").replace("\"", ""); PDFont verdanaI = PDType0Font.load(pdf, new FileInputStream(new File(verdanaIPath)), true);
内容的提问来源于stack exchange,提问作者confusedDeveloper66
相关产品推荐
相关产品推荐

