gpt4 book ai didi

java - createNewFile() 后忽略的方法

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

我从this question复制了方法并修改了一点以在我的项目中使用。我注意到,当我调用方法 createNewFile() 时,应用程序会忽略我方法的其余部分。我该如何解决这个问题?

  public void exportDatabase(){
try{
File dbFile=getDatabasePath("Glukozko.db");
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()){
exportDir.mkdirs();
}
File file = new File(exportDir, "meritve.csv");
file.createNewFile(); //from this point on, the method is ignored
PrintWriter csvWrite = new PrintWriter(new FileWriter(file));
Cursor meritve=db.vrniMeritve();
if(meritve.getCount()==0){
Toast.makeText(MainActivity.this,"No data availible",Toast.LENGTH_SHORT).show();
}else{
while (meritve.moveToNext()){
String vrstica=meritve.getString(1)+";"+meritve.getString(2)+";"+meritve.getString(3)+";"+meritve.getString(4)+";"+meritve.getString(5)+";"+meritve.getString(6);
System.out.println(vrstica);
}
}
}catch (Exception e){

}
}

我找到了解决方案。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

我必须把它放在 manifset 文件中!

最佳答案

file.createNewFile();  //from this point on, the method is ignored

它抛出了一个异常!

然后您继续压缩异常...通过捕获异常并在 catch block 中不执行任何操作。

如果您打印异常的堆栈跟踪而不是压缩它,您可能会发现有关导致异常的原因的重要线索。一旦您知道是什么原因造成的,您就可以找出解决方法。

Also the file is not created

是的。方法调用失败。

关于java - createNewFile() 后忽略的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43658644/

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