【问题标题】:Make a txt file in internal storage in android在android的内部存储中制作一个txt文件
【发布时间】:2014-07-15 06:52:27
【问题描述】:

我想在根目录下创建一个文件夹并放入txt文件并附加数据 '

我的java代码

  public void generateNoteOnSD(String sFileName, String sBody){
        try
        {
            String fileName = "error";
            String headings = "Hello, world!";
            String path = "/data/root/";
            File file = new File(path, fileName+".txt");
            if (!file.exists()) {
                file.mkdirs();
            }
            File gpxfile = new File(file, sFileName);
            FileWriter writer = new FileWriter(gpxfile);
            writer.append(sBody);
            writer.flush();
            writer.close();
           // Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
        }
        catch(IOException e)
        {
            Log.d("file error", ""+e.getMessage());
        }
       }  

我收到文件未找到异常

请帮助我如何在内部存储的根文件夹中创建文件

【问题讨论】:

  • 除非您的手机已root,否则您无法访问该目录(您甚至无权访问 /data 目录)。如果您是 root 用户,您的应用需要以 root 身份运行。

标签: android android-file


【解决方案1】:

试试这个功能。

public void wrtieFileOnInternalStorage(Context mcoContext,String sFileName, String sBody){
    File file = new File(mcoContext.getFilesDir(),"mydir");
    if(!file.exists()){
        file.mkdir();
    }

    try{
        File gpxfile = new File(file, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();

    }catch (Exception e){

    }
}

【讨论】:

  • 将“mydir”更改为您的文件夹名称
  • 但你不能在根文件夹中创建文件夹
  • 以上文件夹是在应用存储中创建的,其他应用无法看到和访问
  • 需要手动打开txt文件但手机里没有找到txt文件
【解决方案2】:

试试这样的 -

public Boolean writeToSD(String text){
        Boolean write_successful = false;
         File root=null;  
            try {  
                // <span id="IL_AD8" class="IL_AD">check for</span> SDcard   
                root = Environment.getExternalStorageDirectory();  
                Log.i(TAG,"path.." +root.getAbsolutePath());  

                //check sdcard permission  
                if (root.canWrite()){  
                    File fileDir = new File(root.getAbsolutePath());  
                    fileDir.mkdirs();  

                    File file= new File(fileDir, "samplefile.txt");  
                    FileWriter filewriter = new FileWriter(file);  
                    BufferedWriter out = new BufferedWriter(filewriter);  
                    out.write(text);  
                    out.close();  
                    write_successful = true;
                }  
            } catch (IOException e) {  
                Log.e("ERROR:---", "Could not write file to SDCard" + e.getMessage());  
                write_successful = false;
            }  
        return write_successful;
    }

从这里 - http://www.coderzheaven.com/2012/09/06/read-write-files-sdcard-application-sandbox-android-complete-example/

【讨论】:

    【解决方案3】:

    在此声明中:

    File gpxfile = new File(file, sFileName);
    

    文件应该是目录(你在这里使用.txt文件)。

    另外,请阅读this。您可以将文件存储在/data/data/&lt;your.package.name&gt;/ 目录中(尝试使用/data/root/ 会导致权限被拒绝错误)。

    【讨论】:

      【解决方案4】:

      试试这个。使用它,我在 SD 卡中创建了日志文件。

      public void writeFile(String text){
      
          File tarjeta = Environment.getExternalStorageDirectory();
          File logFile = new File(tarjeta.getAbsolutePath()+"/", "log.txt");
          if (!logFile.exists())
          {
              try
              {
                  logFile.createNewFile();
              } 
              catch (IOException e)
              {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
          try
          {
              //BufferedWriter for performance, true to set append to file flag
              BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
              buf.append(text);
              buf.newLine();
              buf.close();
          }
          catch (IOException e)
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-14
        • 2013-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多