码迷,mamicode.com
首页 > 移动开发 > 详细

Android中的文件权限操作

时间:2014-07-19 16:12:31      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   java   color   

默认本工程创建的文件本工程对其有读写权限。

我们可以通过context.openFileOutput("文件名", 模式);

 

我们可以创建私有, 共有, 只读, 只写文件, 默认的文私有文件。

如果别的Android工程访问本工程的文件的话就会受限制, android的内核是linux, 所以他的文件管理和linux中的文件时一样的。

 

创建文件代码:

bubuko.com,布布扣
/**
     * 创建各种文件
     * @throws IOException 
     * 
     */
    @SuppressLint({ "WorldWriteableFiles", "WorldReadableFiles" })
    @SuppressWarnings("resource")
    private void createFile() throws IOException {
        Log.i(TAG, "开始创建文件");
        
        FileOutputStream fos;
        
        //创建私有文件 
        fos = this.openFileOutput("private.txt", Context.MODE_PRIVATE);
        fos.write("private".getBytes());
        fos.close();
        
        //创建公有文件
        fos = this.openFileOutput("public.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
        fos.write("public".getBytes());
        fos.close();
        
        //创建默认文件
        File file = new File(this.getFilesDir(), "default.txt");
        fos = new FileOutputStream(file);
        fos.write("default".getBytes());
        fos.close();
        
        //创建只读文件
        fos = this.openFileOutput("readable.txt", Context.MODE_WORLD_READABLE);
        fos.write("readable".getBytes());
        fos.close();
        
        //创建只写文件
        fos = this.openFileOutput("writeable.txt", Context.MODE_WORLD_WRITEABLE);
        fos.write("writeable".getBytes());
        fos.close();
        
        Toast.makeText(this, "文件创建成功", Toast.LENGTH_LONG).show();
        
        Log.i(TAG, "创建文件完成");
    }
View Code

 

 

othera Android工程访问 filespower工程所创建的文件

 

bubuko.com,布布扣
package com.example.other;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener{
    
    private String TAG = "MainActivity";
    
    /** 读写私有文件 */
    private Button btnWritePrivate, btnReadPrivate;
    
    /** 读写共有文件 */
    private Button btnWritePublic, btnReadPublic ;
    
    /** 读写默认文件 */
    private Button btnWriteDefalut, btnReadDefault;
    
    /** 读写只读文件 */
    private Button btnWriteReadable, btnReadReadable;
    
    /** 读写只写文件 */
    private Button btnWriteWriteable, btnReadWriteable;
    
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //组件获取
        btnWritePrivate = (Button)findViewById(R.id.btn_writePrivate);
        btnReadPrivate = (Button)findViewById(R.id.btn_readPrivate);
        
        btnWritePublic = (Button)findViewById(R.id.btn_writePublic);
        btnReadPublic = (Button)findViewById(R.id.btn_readPublic);
        
        btnWriteDefalut = (Button)findViewById(R.id.btn_writeDefault);
        btnReadDefault = (Button)findViewById(R.id.btn_readDefault);
        
        btnWriteReadable = (Button)findViewById(R.id.btn_writeReadable);
        btnReadReadable = (Button)findViewById(R.id.btn_readReadable);
        
        btnWriteWriteable = (Button)findViewById(R.id.btn_writeWriteable);
        btnReadWriteable = (Button)findViewById(R.id.btn_readWriteable);
        
        
        //设置监听事件
        btnWritePrivate.setOnClickListener(this); 
        btnReadPrivate.setOnClickListener(this);
        
        btnWritePublic.setOnClickListener(this);
        btnReadPublic.setOnClickListener(this);
        
        btnWriteDefalut.setOnClickListener(this);
        btnReadDefault.setOnClickListener(this);
        
        btnWriteReadable.setOnClickListener(this);
        btnReadReadable.setOnClickListener(this);

        btnWriteWriteable.setOnClickListener(this);
        btnReadWriteable.setOnClickListener(this);
        
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_readPrivate:
            readFile("private.txt");
            break;
        case R.id.btn_readPublic:
            readFile("public.txt");
            break;
        case R.id.btn_readDefault:
            readFile("default.txt");
            break;
        case R.id.btn_readReadable:
            readFile("readable.txt");
            break;
        case R.id.btn_readWriteable:
            readFile("writeable.txt");
            break;
        case R.id.btn_writePrivate:
            writeFile("private.txt");
            break;
        case R.id.btn_writePublic:
            writeFile("public.txt");
            break;
        case R.id.btn_writeDefault:
            writeFile("default.txt");
            break;
        case R.id.btn_writeReadable:
            writeFile("readable.txt");
            break;
        case R.id.btn_writeWriteable:
            writeFile("writeable.txt");
            break;
            

        default:
            break;
        }
        
    }

    @SuppressLint("SdCardPath")
    public void readFile(String fileName) {
        Log.i(TAG, "读文件"+fileName);
        
        File file = new File("/data/data/com.example.filepower/files/"+fileName);
        try {
            @SuppressWarnings("resource")
            BufferedReader br = new BufferedReader(new FileReader(file));
            String context = br.readLine();
            Toast.makeText(this, context, Toast.LENGTH_SHORT).show();
            
        } catch (Exception e) {
            Toast.makeText(this, "读取失败", Toast.LENGTH_SHORT).show();
        }
        
        
    }
    
    @SuppressLint("SdCardPath")
    @SuppressWarnings("resource")
    public void writeFile(String fileName) {
        Log.i(TAG, "写文件"+fileName);
        File file = new File("/data/data/com.example.filepower/files/", fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write("xxx".getBytes());
        } catch (Exception e) {
            Toast.makeText(this, "写文件失败", Toast.LENGTH_SHORT).show();
        }
    }
}
View Code

 

 

文件讲解:

bubuko.com,布布扣

Android中的文件权限操作,布布扣,bubuko.com

Android中的文件权限操作

标签:android   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/E-star/p/3854861.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!