Java 将数据写入文件

Java 将数据写入文件,java,android,Java,Android,我想从我的应用程序中创建一个文件夹。在文件夹中,我只想创建一个文件(比如recents),应用程序将在每次发布时逐行写入数据 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);

我想从我的应用程序中创建一个文件夹。在文件夹中,我只想创建一个文件(比如recents),应用程序将在每次发布时逐行写入数据

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中的最近文件的内容?当我逐行写入数据时,我想逐行访问数据。如果有人花时间向我解释怎么做,我会非常感激,因为我需要学习。另外,如果我做错了什么,请告诉我。

基于您所做的,我认为使用SharedReference是一种更好的方式。它不会被其他人覆盖,并且对在文件资源管理器中捣乱的人隐藏。这里有一个简单的例子,假设你最近的电话号码是以某种形式排列的。当然,其他方法也是可能的

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);
}
SharedReferences Prefs=getSharedReferences(“最近的电话号码”,模式_PRIVATE);
对于(Map.Entry:Prefs.getAll().entrySet())
{
字符串s=entry.getValue().toString();
RecentPhoneArray.add;
}

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

用于创建自定义目录(此处最近)和自定义文件(此处最近)

以下代码有助于读取同一目录(此处最近)中的同一文件(此处最近)

需要注意的一件重要事情是,如果目录最近出现的和其中的文件最近出现的不存在,那么它将在第一次运行时自动创建。从第二次运行开始,它将开始引用它,而不是重新创建整个过程


希望这将帮助一些用户

当您不需要它时,您正在使用
getApplicationContext()
。另外,您正在将
Context.MODE\u APPEND
传递到
getDir()
,该值不受支持--请使用
0
。我需要帮助。你能告诉我为什么我不需要getApplicationContext吗?我想说的是为什么它在没有上下文的情况下工作。这个方法会写在同一个文件中还是会覆盖文件的内容??我需要做的是在同一个文件上以不同的行继续写,“我想说的是为什么它在没有上下文的情况下工作”——在这些地方你需要一个
上下文。您已经处于
上下文中
。使用
this
而不是
getApplicationContext()
。您的答案包含不正确的信息。SharedReferences与应用程序私有目录中包含的文件一样“安全”,这也是用户提问时使用的文件。没有其他人可以“覆盖”这些文件,也没有人可以使用文件资源管理器查看这些文件,除非手机是根目录的——但在这种情况下,共享引用也可以访问。@323go您是对的,对不起。我不知何故误读了这样一个观点,即OP将目录设置为可访问的目录,例如\sdcard。问题是我必须创建两个不同的文件夹,并在一个文件夹中写入一组文件(每个文件中也有一组要写入的数据)。(在这个文件夹中,每次都会创建一个新文件)。在另一个文件夹中,我必须创建一个包含数据的文件。(在此文件夹中,数据不断添加到原始文件中)。你能给我建议一个最好的方法吗?因为我在编程方面完全是新手。@nishan_vodoo问题是我不知道你在保存和编写什么。基于OP,我仍然认为最好的方法是SyrdPoopy。当您提到有多个文件和多个文件夹时,我会开始想,也许使用SQLLite是一种方法:。它允许根据需要保存和检索数据。现在,如果您知道确实需要读/写文件,只需谷歌代码即可。你似乎已经有一些以上!
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();
}