用Java写入文本文件

用Java写入文本文件,第1张

概述我有一个需要(可能是)经验丰富的Java专业人士的应用.该引擎工作正常,但是我无法将数据保存到文本文件;此外,每次调试文件 *** 作时,由于无法写入模拟器中的磁盘,因此必须束缚平板电脑,卸载旧应用程序,复制apk并重新安装.我补充说:<uses-permissionandroid:name="android.permission.

我有一个需要(可能是)经验丰富的Java专业人士的应用.该引擎工作正常,但是我无法将数据保存到文本文件;此外,每次调试文件 *** 作时,由于无法写入模拟器中的磁盘,因此必须束缚平板电脑,卸载旧应用程序,复制apk并重新安装.

我补充说:

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

到应用程序标记之外的清单,以及尝试了多种写入文件的方法.例外情况是(取决于当前尝试)eacces或文件不存在.我也整日在Google上寻找解决方案.有人知道吗?

// this is a canned method that should presumably work private voID writeStringToTextfile(String s, String f){file sdCard = Environment.getExternalStorageDirectory();file dir = new file (sdCard.getabsolutePath());dir.mkdirs();file file = new file(dir, f);try{    fileOutputStream f1 = new fileOutputStream(file,false); //True = Append to file, false = Overwrite    PrintStream p = new PrintStream(f1);    p.print(s);    p.close();    f1.close();} catch (fileNotFoundException e) {} catch (IOException e) {}   }// this is the save method// it has been modifIEd a little less than a million times today// some lines may be unnecessary because I have trIEd several approaches  public voID saveSystem(VIEw vIEw){          String s="";          String filename = "data/data/topExpertAndroID/" + filename;          fileOutputStream f;          file file = new file(filename);          if(!(file.exists())){             try{                 file.createNewfile();}              catch(Exception e){                showSplash(e.getMessage() + e.getCause(), "OK");                return;}}          try{              getfile();              if(filename.equals("")){}              else{                   f = openfileOutput(filename, Context.MODE_PRIVATE);                   for(int temp=0; temp<50; temp++){                       for(int loop=0; loop<50; loop++){                           s += topExpert.nodeBank[temp][loop].activationResponse + "\n";                           s += topExpert.nodeBank[temp][loop].display + "\n";                           s += topExpert.nodeBank[temp][loop].falselink + "\n";                           s += topExpert.nodeBank[temp][loop].IDentifIEr + "\n";                           if(topExpert.nodeBank[temp][loop].istop){                              s += "true";}                           else{                                s += "false";}                           s += "\n" + topExpert.nodeBank[temp][loop].truelink + "\n";}}                           writeStringToTextfile(s,filename);              }}          catch(Exception e){                showSplash(e.getMessage() + e.getCause(), "OK");}}

解决方法:

filename变量开头可能缺少/:

String filename = "/data/data/topExpertAndroID/" + filename;

而且您可能应该改用getDataDirectory或getExternalStorageDirectory …

但是关于无法写入仿真器磁盘的说法似乎很奇怪……?

总结

以上是内存溢出为你收集整理的用Java写入文本文件全部内容,希望文章能够帮你解决用Java写入文本文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1085989.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存