我想从我的应用程序内部创建文件夹。在文件夹内,我想创建一个文件(比如最近的文件),其中应用程序将在每次启动时一行一行地写入数据。将数据写入文件
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里面的内容是什么?我想逐行访问数据,因为我在逐行写入数据。我真的很感激,如果有人花时间来解释我如何去做,因为我需要学习它。请让我知道如果我做错了什么。
您正在使用'getApplicationContext()'当你不需要它。此外,您将'Context.MODE_APPEND'传递给'getDir()',它不是受支持的值 - 使用'0'。 – CommonsWare 2013-03-04 19:47:06
我需要帮助。你能告诉我为什么我不需要getApplicationContext吗?我的意思是说,为什么它没有上下文就工作。 – nishantvodoo 2013-03-04 19:49:03
这个方法也会写在同一个文件里面还是会覆盖文件的内容?我需要做的是继续在不同的行中写入同一个文件。 – nishantvodoo 2013-03-04 20:19:35