gpt4 book ai didi

android - IllegalArgumentException:文件包含路径分隔符 Android

转载 作者:行者123 更新时间:2023-11-29 19:27:11 24 4
gpt4 key购买 nike

我正在谷歌搜索,但找不到我问题的真正答案!我的问题与 him 相同但是他想要 MODE_APPEND 而我想要我的文件的 MODE_PRIVATE。我该怎么办?

这是我的代码:

public boolean saveCustomButtonInfo (Context context, Vector<DocumentButtonInfo> info) throws Exception{
String path= context.getFilesDir() + "/" + "Load";
File file = new File(path);

if(! file.exists()){
file.mkdir();
//Toast.makeText(context,file.getAbsolutePath(),Toast.LENGTH_LONG).show();
}
path=path+"/DocumentActivityCustomButtonsInfo.obj";
try{
FileOutputStream out=context.openFileOutput(path,Context.MODE_PRIVATE);
ObjectOutputStream outObject=new ObjectOutputStream(out);
outObject.writeObject(info);
outObject.flush();
out.close();
outObject.close();
return true;
}catch(Exception ex){
throw ex;

}
}

最佳答案

您不能将带斜线的路径 (/) 与 openFileOutput() 一起使用。更重要的是,您试图将 getFilesDir()openFileOutput() 结合起来,这是不必要的,并且会导致此问题。

将您的代码更改为:

public void saveCustomButtonInfo (Context context, List<DocumentButtonInfo> info) throws Exception {
File dir = new File(context.getFilesDir(), "Load");

if(! dir.exists()){
dir.mkdir();
}
File f = new File(dir, "DocumentActivityCustomButtonsInfo.obj");
FileOutputStream out=new FileOutputStream(f);
ObjectOutputStream outObject=new ObjectOutputStream(out);
outObject.writeObject(info);
outObject.flush();
out.getFD().sync();
outObject.close();
}

注意事项:

  • Vector 已过时约 15 年
  • 切勿使用串联来构建文件系统路径;使用正确的 File 构造函数
  • 捕获异常只是重新抛出它是没有意义的
  • 返回一个始终为 trueboolean 是没有意义的
  • FileOutputStream 上调用 getFD().sync() 以确认所有字节都已写入磁盘

关于android - IllegalArgumentException:文件包含路径分隔符 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41076602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com