0

您好,我想创建一个文件并在其中写入一些数据,然后我想为该文件分配只读权限,这样其他人就无法修改它。

我已经完成了写作部分,但我不知道如何设置只读权限并删除我尝试过的其他所有权限 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();
4

1 回答 1

3

如果文件在内部存储中(例如,getFilesDir()),则默认为只读。

如果文件在外部存储(例如,Environment.getExternalStorageDirectory())上,AFAIK 您不能更改权限位以使其只读。

于 2013-07-04T17:54:07.267 回答