在系统中找不到文件,创建和读取它通过成功java

时间:2013-11-10 07:42:43

标签: java filereader filewriter

有一个代码通过给定名称写入文件。 有一个从它读取的哈希映射。 写作成功,也是从中读取。

但文件无处可寻。

这是写的代码:

public void attachUrlToGuid(String url, String guid) {
    ObjectMapper mapper = new ObjectMapper();
    fileList.put(url, guid);// doesn't matter if url already exist or have a different guid - just replacing with the new value.
    try{
        fileWriter = new FileWriter(fileName);
        mapper.writeValue(fileWriter, fileList);
    }
    catch (IOException e){//in case of failure and the file did not get updated, then in the next upload file it will be written due to keeping HM alive.
        LOGGER.error(ERROR_MSG_WRITE_TO_FILE);
    }
}

以下是阅读:

static{
    ObjectMapper mapper = new ObjectMapper();
    try{
        fileReader = new FileReader(fileName);
        fileList = mapper.readValue(fileReader, HashMap.class);
    }
    catch (FileNotFoundException e){//in case file does not exist
        fileList = new HashMap<String, String>();
    }
    catch (IOException e){
        LOGGER.error(ERROR_MSG_INVALID_DATA);
    }
}

小心解释?

1 个答案:

答案 0 :(得分:0)

只需查看new File(url).exists();

即可