2013-03-04 172 views
-1

我想从我的应用程序内部创建文件夹。在文件夹内,我想创建一个文件(比如最近的文件),其中应用程序将在每次启动时一行一行地写入数据。将数据写入文件

private void saveForRecents(String phoneNumber) { 
    //Need to open File and write the phonenumbers that has been passed into it 
    try{  
     File mydir = getDir("recents", 0); 

     //Creating an internal dir; 
     File fileWithinMyDir = new File(mydir, "recents"); 

     //Getting a file within the dir. 
     FileWriter fw = new FileWriter(fileWithinMyDir.getAbsoluteFile()); 


     BufferedWriter bw = new BufferedWriter(fw); 
     bw.write(phoneNumber); 
     bw.newLine(); 
     bw.flush(); 
     bw.close(); 

    }catch(Exception e){ 
     Toast.makeText(getApplicationContext(), "Failed to write into the file", Toast.LENGTH_LONG).show(); 
    } 
} 

如何访问最近用过文件,它是我的目录MYDIR里面的内容是什么?我想逐行访问数据,因为我在逐行写入数据。我真的很感激,如果有人花时间来解释我如何去做,因为我需要学习它。请让我知道如果我做错了什么。

+0

您正在使用'getApplicationContext()'当你不需要它。此外,您将'Context.MODE_APPEND'传递给'getDir()',它不是受支持的值 - 使用'0'。 – CommonsWare 2013-03-04 19:47:06

+0

我需要帮助。你能告诉我为什么我不需要getApplicationContext吗?我的意思是说,为什么它没有上下文就工作。 – nishantvodoo 2013-03-04 19:49:03

+0

这个方法也会写在同一个文件里面还是会覆盖文件的内容?我需要做的是继续在不同的行中写入同一个文件。 – nishantvodoo 2013-03-04 20:19:35

回答

0

根据你在做什么,我认为使用SharedPreferences是一个更好的方法。它不会被其他人覆盖,并且它隐藏在文件浏览器中的某个人身上。这里有一个简单的例子,假设你在某个数组中有最近的电话号码。其他方法当然是可能的。

SharedPreferences Prefs = getSharedPreferences("RecentPhoneNumbers", MODE_PRIVATE); 
Editor e = Prefs.edit(); 
e.clear(); 

for (String s : RecentPhoneArray) 
{ 
    e.putString(s); 
} 
e.commit(); 

然后到下一次加载它们的应用程序启动后或者需要重载:

SharedPreferences Prefs = getSharedPreferences("RecentPhoneNumbers", MODE_PRIVATE); 
for (Map.Entry<String, ?> entry : Prefs.getAll().entrySet()) 
{ 
    String s = entry.getValue().toString(); 
    RecentPhoneArray.add(s); 
} 
+0

您的回答包含不正确的信息。 SharedPreferences与应用程序专用目录中包含的文件一样“安全”,这是用户提出问题的用途。没有其他人可以“覆盖”这些文件,也没有人可以使用文件浏览器对它们进行捅破,除非电话是根植的 - 但在这种情况下,SharedPreferences也可以访问。 – 323go 2013-03-04 20:21:56

+0

@ 323go你是对的,对不起。我不知何故误读,认为OP正在创建一个目录可访问的任何地方\ sdcard – Paul 2013-03-04 22:27:58

+0

问题是,我必须创建两个不同的文件夹,并在一个文件夹内写入一组文件(每个文件也有一组数据写在里面)(在这个文件夹中每次创建一个新文件)。在另一个文件夹中,我必须创建一个包含数据的文件(在这个文件夹中,数据不断被添加到原始文件中)。你能否建议我最好的方式来做到这一点,因为我是一个完全新手,当谈到编程。 – nishantvodoo 2013-03-04 23:53:59

0

后很长一段时间我想通了,我的问题的第二部分。如果遇到同样的问题,这可能对其他用户有用。

制作自定义目录(这里最近用过)和一个自定义文件(这里最近

try 
{ 
     File mydir = getDir("recents", 0); 
     File fileWithinMyDir = new File(mydir, "recent"); 
     //true here lets the data to be added in the same file in the next line 
     // No value at all or a false will overwrite the file 
     FileWriter fw = new FileWriter(fileWithinMyDir.getAbsoluteFile(), true); 
     BufferedWriter bw = new BufferedWriter(fw); 
     bw.write("Anything you want to write"); 
     bw.newLine(); 
     bw.flush(); 

    bw.close(); 

    }catch(Exception e){ 

    Toast.makeText(getApplicationContext(), "Failed to write into the file", Toast.LENGTH_LONG).show(); 
} 

下面的代码有助于读取相同的文件(这里最近)内的同目录(这里最近用过

try 
{ 

    File mydir = this.getDir("recents", 0); 
    File fileWithinMyDir = new File(mydir, "recent"); 
    try 
    { 
    // open the file for reading 
    InputStream instream = new FileInputStream(fileWithinMyDir); 


    if (instream != null) 
    { 
     // prepare the file for reading 
     InputStreamReader inputreader = new InputStreamReader(instream); 
     BufferedReader buffreader = new BufferedReader(inputreader); 

     String line; 

     while ((line = buffreader.readLine())!= null) 
     { 

     // do something with the line 

     } 

     instream.close(); 
    } 
    } 
    catch (Exception ex) 
    { 
    // print stack trace. 
    } 
    finally 
    { 
    // close the file. 

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

需要注意的一个重要的事情是我F A目录最近用过这里和文件里面最近这里不存在,那么它会自动在第一次运行产生。从第二轮就开始引用它,而不是再重新创建整个事情...

希望这将帮助一些用户