找不到文件异常2

时间:2015-09-01 07:34:43

标签: java filenotfoundexception

String filename = new String(APDirectory.META_DATA.getPath() + getDefaultFileName() + ".ini");
File iniFile = new File(filename);

try {
    Wini ini = new Wini(iniFile);
    ini.put(title, "Comment:", comment);
    ini.put(title, "Position:", rectangle.x + "," + rectangle.y + "," + rectangle.width + "," + rectangle.height);
    ini.store();
} catch (InvalidFileFormatException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

static String getDefaultFileName() {
    String output = new String(ImageInfo.imageName);

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
    output += "_" + dateFormat.format(new Date());

    return output;
}

我创建了一个新文件但是出现以下错误:

  

java.io.FileNotFoundException:   C:\ Users \ XXX \ AP \ MetaData \ CB_2015-09-01_07-31-01.ini(Sistem belirtilen   dosyayıbulamıyor)at java.io.FileInputStream.open0(Native Method)     在java.io.FileInputStream.open(未知来源)at   java.io.FileInputStream。(未知来源)

1 个答案:

答案 0 :(得分:1)

String filename = new String(APDirectory.META_DATA.getPath() + getDefaultFileName() + ".ini");
System.out.println(APDirectory.META_DATA.getPath() + getDefaultFileName() + ".ini");
File iniFile = new File(filename);

Wini ini;

try {
    iniFile.createNewFile();
    ini = new Wini(iniFile);
    ini.put(title, "Comment:", comment);
    ini.put(title, "Position:", rectangle.x + "," + rectangle.y + "," + rectangle.width + "," + rectangle.height);
    ini.store();
} catch (InvalidFileFormatException e) {
// TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

我必须使用iniFile.createNewFile();

显式创建文件

Wini构造函数不会自动执行此操作。