我无法写入Android设备或模拟器的内部存储。 我可以写入外部存储目录中的文件。
我得到了这个例外 EACCES(许可被拒绝) 在createNewFile调用。
WRITE_EXTERNAL_STORAGE权限位于我的清单
中的正确位置public static Boolean writeFile(String filename, String data)
{
try
{
File file = new File(filename);
// If file does not exists, then create it
if (!file.exists())
{
Log.d("MyLog","file doesn't exist, creating");
file.getParentFile().mkdirs();
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(data);
bw.close();
//Log.d("Success","Success");
return true;
}
catch (IOException e)
{
e.printStackTrace();
Log.d("MyLog","writeFile fail, error:"+e.getMessage());
return false;
}
}
我尝试了很多路径:
filename=Environment.getRootDirectory()+"/"+"test.dat";
filename=Environment.getDataDirectory()+"/"+"test.dat";
filename="data/data/com.android.emsi/testnew.txt";
唯一可行的是: 文件名= Environment.getExternalStorageDirectory()+" /" +" TEST.DAT&#34 ;;
答案 0 :(得分:0)
试试这个
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}