在Android中编写文件

时间:2012-12-10 21:57:32

标签: java android string file

首先,如果存在类似的帖子,我很抱歉,但我无法理解。

我正在尝试在android中编写一个文件,我得到了以下代码:

String FILENAME = "hello_file.txt";
String string = "hello world!";

FileOutputStream fos;
try {
    fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(string.getBytes());
    fos.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
    Log.d("Angles", "FileNotFoundException");
}catch(IOException e){
    e.printStackTrace();
    Log.d("Angles", "IOException");
}

我没有得到任何异常所以我认为它有效,但我无法在设备中找到hello_file.txt文件。

我正在使用Galaxy Nexus;没有sd。

任何帮助?

非常感谢你!

1 个答案:

答案 0 :(得分:0)

如果要在SD卡中创建文件,则需要使用

File f =  new File (Environment.getExternalStorageDirectory(),"myfile.txt")