未指定路徑,您的文件將被保存在您的應用空間(/data/data/your.app.name/
)。
因此,你最好將文件保存到外部存儲(這不一定是SD卡,它可以是默認存儲)。
你可能想挖入主題,通過讀取official docs
在合成:
添加此權限將您的清單:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
它包括讀取權限,所以沒有需要指定它。
保存在指定的位置文件(這是從我的生活鱈拍攝,所以我敢肯定它的工作原理):
public void writeToFile(String data)
{
// Get the directory for the user's public pictures directory.
final File path =
Environment.getExternalStoragePublicDirectory
(
//Environment.DIRECTORY_PICTURES
Environment.DIRECTORY_DCIM + "/YourFolder/"
);
// Make sure the path directory exists.
if(!path.exists())
{
// Make it, if it doesn't exit
path.mkdirs();
}
final File file = new File(path, "config.txt");
// Save your stream, don't forget to flush() it before closing it.
try
{
file.createNewFile();
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(data);
myOutWriter.close();
fOut.flush();
fOut.close();
}
catch (IOException e)
{
Log.e("Exception", "File write failed: " + e.toString());
}
}
[編輯] OK嘗試這樣的(不同的道路 - 在外部存儲的文件夾):
String path =
Environment.getExternalStorageDirectory() + File.separator + "yourFolder";
// Create the folder.
File folder = new File(path);
folder.mkdirs();
// Create the file.
File file = new File(folder, "config.txt");
您是否確實需要將某些東西寫入txt文件?如果不考慮使用SharedPreferences。 http://developer.android.com/guide/topics/data/data-storage.html#pref – Sebastian