我是EMF的新手,正在尝试按以下方式保存EMF模型:
public void saveData(File file, Device device) throws IOException {
final ResourceSet resourceSet = new ResourceSetImpl();
// Use XMI resource
System.out.println("file path in saveData " +file.getPath());
Resource xmiResource = resourceSet.createResource(URI.createFileURI(file.getPath() + ".xmi"));
xmiResource.getContents().add(device);
xmiResource.save(null);
// Use XML resource instead
Resource xmlResource = resourceSet.createResource(URI.createFileURI(file.getPath() + ".xml"));
xmlResource.getContents().add(device);
xmlResource.save(null);
}
但是在指定的路径中没有创建任何文件。加载代码如下:
public Device loadData(String fileName) {
final ResourceSet resourceSet = new ResourceSetImpl();
// Use XMI resource
Resource xmiResource;
System.out.println("filename" + fileName);
try {
xmiResource = resourceSet.getResource(URI.createFileURI(fileName + ".xmi"),true);
xmiResource.load(null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return (Device)xmiResource.getContents().get(0);
}
出现以下错误: java.lang.RuntimeException: 无法为“file:/C:/Users/Desktop/filename.xmi”创建资源;需要注册资源工厂。