发布于 2016-06-29 22:44:39 | 223 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android 访问文件权限的四种模式介绍的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下

Linux文件的访问权限

* 在Android中,每一个应用是一个独立的用户
* drwxrwxrwx
* 第1位:d表示文件夹,-表示文件
* 第2-4位:rwx,表示这个文件的拥有者(创建这个文件的应用)用户对该文件的权限
* r:读
* w:写
* x:执行

* 第5-7位:rwx,表示跟文件拥有者用户同组的用户对该文件的权限

* 第8-10位:rwx,表示其他用户组的用户对该文件的权限

openFileOutput的四种模式

* MODE_PRIVATE:-rw-rw----

* MODE_APPEND:-rw-rw----

* MODE_WORLD_WRITEABLE:-rw-rw--w-

* MODE_WORLD_READABLE:-rw-rw-r--

下面实战一下:

首先完成布局


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"


xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
android:onClick="click1" />
</LinearLayout>

添加按钮事件


public void click1(View v) {
//data/data/com.wuyudong.permission.files
try {
FileOutputStream fos = openFileOutput("info1.txt", MODE_PRIVATE);
fos.write("私有模式".getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

点击按钮后生成相应的文件info1.txt,如图


然后再生成其他的按钮布局:


相应的代码如下:


package com.wuyudong.permission;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click1(View v) {
// data/data/com.wuyudong.permission.files
try {
FileOutputStream fos = openFileOutput("info1.txt", MODE_PRIVATE);
fos.write("私有模式".getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void click2(View v) {
// data/data/com.wuyudong.permission.files
try {
FileOutputStream fos = openFileOutput("info2.txt", MODE_APPEND);
fos.write("追加模式".getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void click3(View v) {
// data/data/com.wuyudong.permission.files
try {
FileOutputStream fos = openFileOutput("info3.txt", MODE_WORLD_READABLE);
fos.write("全局可读模式".getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void click4(View v) {
// data/data/com.wuyudong.permission.files
try {
FileOutputStream fos = openFileOutput("info4.txt", MODE_WORLD_WRITEABLE);
fos.write("私有模式".getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

依次点击按钮,生成相应权限的文件:


再创建一个应用来读取之前生成的info3.txt文件


package com.wuyudong.other;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v) {
File file = new File("data/data/com.wuyudong.permission/files/info3.txt");
try {
FileInputStream fis = new FileInputStream(file);
//把字节流转换成字符流
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String text = br.readLine();
Toast.makeText(this, text, 0).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

以上所述是小编给大家介绍的Android 访问文件权限的四种模式的相关内容,希望对大家有所帮助,如果大家想了解更多内容敬请关注phperz网站!



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务