【发布时间】:2021-08-01 14:37:11
【问题描述】:
我的 CodenameOne 应用正在 iOS 模拟器(iPad 8th iOS 14)上进行测试。
它通过这种方法在私有文件夹中写入一些文件:
public void writeFile() throws IOException {
try(OutputStream os = FileSystemStorage.getInstance().openOutputStream(Utils.getRootPath()+DATA_FILE);)
{
os.write(JSONText.getBytes("UTF-8"));
os.flush();
os.close();
} catch(IOException err) {
System.out.println("exception trying to write");
}
}
它适用于 CN 模拟器(写入 .cn1/ 文件夹内) 但在 iOS 上,异常被捕获。 Library 文件夹在 iOS 上至关重要。
下面是获取根路径的方法
public static String getRootPath()
{
String documentsRoot=FileSystemStorage.getInstance().getRoots()[0];
String os=Display.getInstance().getPlatformName();
if (os.toLowerCase().contains("ios")) {
int pos=documentsRoot.lastIndexOf("Documents");
if (pos==-1) return documentsRoot+"/";
String libraryRoot=documentsRoot.substring(0,pos)+"Library";
String result=libraryRoot+"/";
return result;
}
我的应用程序的 CN 版本必须将这些私有文件写入与 swift 版本相同的位置,即库。
有字符串操作,没有添加额外的'/',文件路径看起来合法。
所以字符串
file:///Users/mac/Library/Developer/CoreSimulator/Devices/alphanumeric-string/data/Containers/Data/Application/another-alphanumeric-string/Documents/
被转换并且 getRootPath() 方法返回
file:///Users/mac/Library/Developer/CoreSimulator/Devices/alphanumeric-string/data/Containers/Data/Application/another-alphanumeric-string/Library/
但也有例外。
此外,在尝试写入后的某个时间点,我在控制台输出中看到了一些我认为相关的内容:
Failed to create directory /Users/mac/Library/Developer/CoreSimulator/Devices/alphanumeric-string/data/Containers/Data/Application/another-alphanumeric-string/Documents/cn1storage/
这是什么?和我的问题有关吗?
CN 文件系统访问是否损坏或有缺陷?
我知道 io 访问权限是由 CN 编译器自动创建的,但它们是否有效?
那么如何解决我关于 Library 文件夹的问题?
【问题讨论】:
标签: java ios exception ios-simulator codenameone