【问题标题】:Android: Write text to txtAndroid:将文本写入 txt
【发布时间】:2013-06-03 17:24:48
【问题描述】:

使用以下代码,我尝试写入我的 sdcard:

public void writedata(String data) {
          //BufferedWriter out = null;

          System.out.println(data);
          try{

              FileOutputStream out = new FileOutputStream(new File("/sdcard/tsxt.txt"));
              out.write(data.getBytes());
              out.close();  

                } catch (Exception e) { //fehlende Permission oder sd an pc gemountet}
                    System.out.println("CCCCCCCCCCCCCCCCCCCCCCCALSKDJLAK");
                }

          }

Manifest 中的权限:

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

但是现在,当我打开文件时,里面什么都没有。问题出在哪里?我确信data 有一定的价值。

编辑:

我在 LogCat 中收到这条消息:

02-06 01:59:51.676: W/System.err(1197): java.io.FileNotFoundException: /storage/sdcard0/sdcard/tsxt.txt: open failed: ENOENT (No such file or directory)

我尝试在 sdcard 上创建文件,但仍然出现同样的错误。如果文件不存在,是否有创建文件的代码?

【问题讨论】:

  • 你的清单中有这个吗?
  • BufferedWriter 更适合编写文本。 (mkyong.com/java/…)
  • 使用 BufferedWriter 的解决方案也不起作用!
  • 您不应硬编码外部存储 (SDCard) 的路径。使用getExternalStorageDirectory()
  • 你的问题解决了吗?

标签: android file text fileoutputstream


【解决方案1】:

试试这个代码:

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/dir");

File file = new File(dir, "tsxt.txt");

FileOutputStream f = new FileOutputStream(file);

所以文件的路径不正确。您应该删除目录名称:

File dir = new File (sdCard.getAbsolutePath() + "/");

【讨论】:

  • 我在 LogCat 中收到这条消息:02-06 01:59:51.676: W/System.err(1197): java.io.FileNotFoundException: /storage/sdcard0/sdcard/tsxt.txt:打开失败:ENOENT(没有这样的文件或目录)
【解决方案2】:

试试这个:

BufferedWriter out;         
try {

FileWriter fileWriter= new FileWriter(Environment.getExternalStorageDirectory().getPath()+"/tsxt.txt")

out = new BufferedWriter(fileWriter);

out.write("Your text to write");

out.close(); 

}catch (FileNotFoundException e) {
 e.printStackTrace();
}catch (IOException e) {
 e.printStackTrace();
}

【讨论】:

  • 不幸的是仍然存在同样的问题。该文件是空的(我之前创建过)
  • 仍然出现 java.io.FileNotFoundException?
  • 只是想知道,您是否已在海量存储模式下安装了设备存储或将其与 PC 连接?
【解决方案3】:
  try{
    File myFile = new File("/sdcard/tsxt.txt");
    myFile.createNewFile();
    FileOutputStream fOut = new FileOutputStream(myFile);
    OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
    myOutWriter.append("your data here");
    myOutWriter.close();
    fOut.close();
}catch(Exception e){}

【讨论】:

    【解决方案4】:

    试试这个

     FileOutputStream fOut =openFileOutput(Environment.getExternalStorageDirectory().getPath()+"/tsxt.txt",MODE_WORLD_READABLE);
     OutputStreamWriter osw = new OutputStreamWriter(fOut);
    //---write the string to the file---
     osw.write(str);
     osw.flush();
     osw.close();
    

    【讨论】:

      【解决方案5】:

      在将任何文件写入 sd 卡之前,您需要检查是否已挂载 sdcard,如果没有,则只需挂载它,然后使用外部存储路径将文件写入其上。

      您可以使用以下代码检查是否安装了 sdcard

      static public boolean hasStorage(boolean requireWriteAccess) {
          //TODO: After fix the bug,  add "if (VERBOSE)" before logging errors.
          String state = Environment.getExternalStorageState();
          Log.v(TAG, "storage state is " + state);
      
          if (Environment.MEDIA_MOUNTED.equals(state)) {
              if (requireWriteAccess) {
                  boolean writable = checkFsWritable();
                  Log.v(TAG, "storage writable is " + writable);
                  return writable;
              } else {
                  return true;
              }
          } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
              return true;
          }
          return false;
      }
      
      if(hasStorage(true)){
      
      //call here your writedata() function
      
      
      
      }
      

      【讨论】:

        【解决方案6】:

        此代码运行良好..

        public File getAlbumStorageDir(String albumName) {
                // Get the directory for the user's public pictures directory.
                File file = new File(Environment.getExternalStorageDirectory() + "/files", albumName);
               Log.d("File", "Bug file Created" + file.getAbsolutePath());
                return file;
            }`
        

        将文本文件写入 sd Card(/storage/sdcard0/files/bugReport.txt)

         try{
                     outputStream = new FileOutputStream(this.getAlbumStorageDir(bugReport.txt));
                     outputStream.write(report.toString().getBytes());
                     Log.d("File","Report Generated");
                     outputStream.close();
                 }
                 catch(Exception e){
                     e.printStackTrace();
                 }
        

        【讨论】:

          猜你喜欢
          • 2020-06-27
          • 1970-01-01
          • 1970-01-01
          • 2016-01-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多