【问题标题】:Writing to text file in Java用Java写入文本文件
【发布时间】:2012-12-11 16:56:54
【问题描述】:

我有一个应用程序需要一位经验丰富的 Java 专业人士(可能是一目了然)。引擎运行良好,但我无法将数据保存到文本文件;而且,每次调试文件操作时,我都必须绑定我的平板电脑,卸载旧应用程序,复制apk并重新安装,因为我无法在模拟器中写入磁盘。

我补充说:

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

到应用程序标签之外的清单,并尝试了许多写入文件的方法。例外是(取决于当前尝试)eacces 或文件不存在。我也整天在谷歌上寻找解决方案。有人知道吗?

// 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");}}

【问题讨论】:

  • 天哪,我多么喜欢那些空的catch-blocks。用e.printStackTrace() 填写它们(两者都!)并将 StackTrace 添加到您的问题中。

标签: java android file save


【解决方案1】:

FILENAME 变量可能在开头缺少/

String FILENAME = "/data/data/TopExpertAndroid/" + fileName;

您可能应该改用getDataDirectorygetExternalStorageDirectory...

但是你所说的无法写入模拟器的磁盘似乎很奇怪......?

【讨论】:

  • ...我不敢相信。是的,就是这样(路径试图在 write 方法中加载为文件)。我如何将其标记为已回答并在到期时给予信用?
  • 很高兴它有帮助......有时像这样的小事情会让你把头发拉出来...... :)答案左侧应该有一个复选标记以接受它。
猜你喜欢
  • 1970-01-01
  • 2016-08-15
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-04
  • 2012-04-15
相关资源
最近更新 更多