未指定路径,您的文件将被保存在您的应用空间(/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