无法在DDMS视图中找到文件

时间:2014-03-09 18:31:36

标签: android file-io

我根据我的时间表为课程生成通知。在通知中,我提供了操作按钮,如果用户单击此按钮,将打开该类的文件,其中将为相应的班级标记出勤。我写了以下代码

   public class Record extends BroadcastReceiver
    {public void onReceive(Context context, Intent intent) 
          {
         String f = intent.getStringExtra("class");

     String fname= f+".txt";
     System.out.println("File Name :" + fname);
     try 
        {   File name = new File(f+".txt");      
            if(!name.exists())
            {
                System.out.println("Creating file" + fname);

                name.createNewFile();
                FileOutputStream fos = context.openFileOutput(fname,Context.MODE_PRIVATE);
                String str1= "1";
                fos.write(str1.getBytes());
                fos.close();
                       if(!name.exists())
                {
                    System.out.println("FILE NOT CREATED");
                }
            }

            else
            {    String inputString;
                 BufferedReader inputReader = new BufferedReader(new InputStreamReader(
                            context.openFileInput(fname)));
                    StringBuffer stringBuffer = new StringBuffer();                
                    while ((inputString = inputReader.readLine()) != null) 
                    {
                           stringBuffer.append(inputString + "\n");
                    }

                   int number = Integer.parseInt(inputString);
                   number=number+1;
                   String str1 = String.valueOf(number);
                   FileOutputStream fos = context.openFileOutput(fname,Context.MODE_PRIVATE);
                   fos.write(str1.getBytes());
                   fos.close();                                        
            }

        }

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

所以我做的是检查文件是否存在。如果没有,那么创建一个else将内容递增一个。

当我运行这个并点击动作按钮时,我明白了 1)文件名 2)创建文件

但不要创建文件。这意味着正在创建文件。但我无法在DDMS视图中找到该文件。

我不知道我哪里错了。请帮忙。

2 个答案:

答案 0 :(得分:0)

尝试将fname放在磁盘上,因为我怀疑你是在磁盘上的非授权区域创建文件

使用

String fname= getFilesDir() +"/" + f+".txt";

答案 1 :(得分:0)

可能已经创建了。 Context.MODE_PRIVATE会自动在您的内部应用程序目录中创建该文件(如果尚未存在)。内部应用文件目录的完整路径为/data/data/com.example.yourpackage/files

您的手机是否已植根?因为如果没有root用户,您应该无法在DDMS中看到根文件夹。如果是这样,您可以检查的另一种方法是在手机上使用File Manager程序。我使用ES File Manager

相关问题