您好我想创建一个文件并在其中写入一些数据,之后我想为该文件分配只读权限,这样其他任何人都无法修改它。
我写完了部分内容 但我不知道如何设置只读权限和删除其他所有权限 我试过setReadOnly();但它不起作用。
如果有任何解决方案,请告诉我。 先谢谢你...
我试过跟随,但它不起作用
String currentDateTimeString;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");//date format
currentDateTimeString = sdf.format(new Date());//current date
File recordFolder = new File(Environment.getExternalStorageDirectory(), "WBS/Record");
recordFolder.mkdirs(); // <----make a folder
File file = new File(recordFolder, currentDateTimeString+".xml");//create the images
File dbtest =new File("/data/data/com/ami/wbs/databases/WBS");
if(!dbtest.exists())
{
// what to do if it does exist
// Toast.makeText(getApplicationContext(), "DB is present", Toast.LENGTH_LONG).show();
}
else
{
// what to do if it doesn't exist
//Toast.makeText(getApplicationContext(), "DB is not present", Toast.LENGTH_LONG).show();
}
database = new DB_Adapter(getApplicationContext(), "WBS", null, 1);
FileWriter filewrite = null;
//BufferedWriter filewrite = null;
try {
//filewrite = new BufferedWriter(new OutputStreamWriter(openFileOutput(currentDateTimeString+".xml", MODE_PRIVATE)));
filewrite = new FileWriter(file,true);
filewrite.write("<?xml version='1.0' ?>");
filewrite.write("<Resultset>");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SQLiteDatabase db=database.getReadableDatabase();
Cursor c = db.rawQuery("Select CONSUMER_ID,METER_NO,PREV_READING,CURR_READING from CONSUMER;", null);
if(c.moveToNext())
{
// Toast.makeText(getApplicationContext(),"Table found", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"Table not found", Toast.LENGTH_SHORT).show();
}
c.moveToFirst();
do
{
String consumerid=c.getString(c.getColumnIndex("CONSUMER_ID"));
String meterno=c.getString(c.getColumnIndex("METER_NO"));
String currentreading=c.getString(c.getColumnIndex("CURR_READING"));
try {
filewrite.write("<consumer>"
+"<consumerid>"+consumerid+"</consumerid>"
+"<meter>"+meterno+"</meter>"
+"<curread>"+currentreading+"</curread></consumer>");
filewrite.write("</Resultset>");
filewrite.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}while(c.moveToNext());
file.setReadOnly();
Toast.makeText(getApplicationContext(), "Export file successfully", Toast.LENGTH_SHORT).show();
答案 0 :(得分:3)
如果文件位于内部存储空间(例如getFilesDir()
),则默认情况下只读取该文件。
如果文件位于外部存储设备(例如Environment.getExternalStorageDirectory()
),则AFAIK无法更改权限位以使其成为只读。