字体文件不会删除

时间:2013-04-08 00:40:13

标签: java file delete-file

我正在尝试安装PhpStorm,经过漫长的故事,我看起来有些坏字体妨碍了Java。

我对Java完全不熟悉,但是我发现这段代码可以遍历我的字体并找到不好的字体。我已将其修改为删除坏字体,但不会删除。

import java.io.File;
import java.awt.Font;
import java.awt.GraphicsEnvironment;

public class myFontCheck {
  public static void main(String[] args) {
    Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
    for (int i = 0; i < fonts.length; i++) {
      final Font font = fonts[i];
      final String name = font.getFontName();

      if (font.canDisplay('a') &&
        font.canDisplay('z') &&
        font.canDisplay('A') &&
        font.canDisplay('Z') &&
        font.canDisplay('0') &&
        font.canDisplay('1')) {
        //System.out.println(" OK.");
      } else {
        File file = new File("c:\\Windows\\Fonts\\" + name + ".ttf");
        if(file.exists()) {
          System.out.println("Bad Font: " + name);
          file.delete();
        }
      }
    }
  }
}

我认为这是文件权限冲突,但我无法弄清楚如何更改权限。我需要以管理员身份运行吗?

或..你能告诉我如何在浏览器窗口中打开坏文件,以便我可以“全选”吗?

1 个答案:

答案 0 :(得分:1)

if (font.canDisplay('a') &&
    font.canDisplay('z') &&
    font.canDisplay('A') &&
    font.canDisplay('Z') &&
    font.canDisplay('0') &&
    font.canDisplay('1')) {
    //System.out.println(" OK.");
  } else {
    System.out.println("Bad Font: " + name);
    File file = new File("c:\\Windows\\Fonts\\" + name + ".ttf");
    file.delete();
  }

取消注释“坏字体行”。运行应用程序时是否打印出任何内容?我怀疑没有,因为虽然你可能不喜欢字体显示的方式,但canDisplay可能会返回true。

或者,“新文件”,字符串构造可能会导致错误。我也会尝试打印出来,并检查文件系统中的文件。

有点像这样:

  } else {
    System.out.println("Bad Font: " + name);
    String fn = "c:\\Windows\\Fonts\\" + name + ".ttf";
    System.out.println("Trying to delete: " + fn);
    File file = new File(fn);
    file.delete();
  }