文件访问的权限

来源:互联网 发布:网络充值卡代理 编辑:程序博客网 时间:2024/06/07 23:31

被访问的文件中:

布局文件:

<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"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="请输入用户名" />    <EditText        android:id="@+id/et_usename"        android:layout_width="fill_parent"        android:layout_height="wrap_content" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="请输入密码" />    <EditText        android:inputType="textPassword"        android:id="@+id/password"        android:layout_width="fill_parent"        android:layout_height="wrap_content" />    <RelativeLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content" >        <CheckBox            android:id="@+id/cb_remenber_passward"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:checked="true"            android:text="记住密码" />        <Button            android:layout_alignParentRight="true"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="login"            android:text="登陆" >        </Button>    </RelativeLayout></LinearLayout>
   类文件:MainActivity
package com.example.shenchao2;import android.app.Activity;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.RadioGroup;import android.widget.Toast;import com.example.shenchao2.service.LoginService;public class MainActivity extends Activity {private static final String TAG = "MainActivity";private EditText ed_usename;private EditText ed_password;private CheckBox cb;private RadioGroup rg_mode;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ed_usename = (EditText) this.findViewById(R.id.et_usename);ed_password = (EditText) this.findViewById(R.id.password);cb = (CheckBox) this.findViewById(R.id.cb_remenber_passward);rg_mode = (RadioGroup) findViewById(R.id.rg_mode);}/** * 登陆成功 */public void login(View vew) {String usename = ed_usename.getText().toString().trim();String password = ed_password.getText().toString().trim();// 将其内容拿出来// 判断是否为空if (TextUtils.isEmpty(usename) || TextUtils.isEmpty(password)) {Toast.makeText(this, "不能为空", Toast.LENGTH_LONG).show();} else {// 登陆,是否保存密码if (cb.isChecked()) {// 保存用户密码// Log.i(TAG, "需要保存用户密码!");// 日志文件信息boolean result = false;int id = rg_mode.getCheckedRadioButtonId(); // 拿到当前的RadioButton的idswitch (id) {case R.id.rd_private:result = LoginService.saveUseInfo(this, usename, password,1);break;case R.id.rd_read:result = LoginService.saveUseInfo(this, usename, password,2);break;case R.id.rd_write:result = LoginService.saveUseInfo(this, usename, password,3);break;case R.id.rd_public:result = LoginService.saveUseInfo(this, usename, password,4);break;default:break;}if (result) {Toast.makeText(this, "好高兴,保存成功!", Toast.LENGTH_LONG).show();}}// 登陆发送消到服务器,服务器验证是否正确if ("zhangsan".equals(usename) && "123".equals(password)) {Toast.makeText(this, "登陆成功!", Toast.LENGTH_LONG).show();} else {Toast.makeText(this, "登陆失败!", Toast.LENGTH_LONG).show();}}}}

LoginService:

package com.example.shenchao2.service;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;import android.content.Context;public class LoginService {public static boolean saveUseInfo(Context context, String usename,String password, int mode) {try {// 文件名、以什么方式创建出来文件FileOutputStream fos = null;switch (mode) {case 1:fos = context.openFileOutput("private.txt",Context.MODE_PRIVATE);break;case 2:fos = context.openFileOutput("read.txt",Context.MODE_WORLD_READABLE);break;case 3:fos = context.openFileOutput("write.txt",Context.MODE_WORLD_WRITEABLE);break;case 4:fos = context.openFileOutput("public.txt",Context.MODE_WORLD_READABLE+ Context.MODE_WORLD_WRITEABLE);break;default:break;}// 将文件写入fos.write((usename + "##" + password).getBytes());// getBytes()是将一个字符串转化为一个字节数组。fos.close();return true;} catch (Exception e) {e.printStackTrace();return false;}}}

在访问的程序中:

布局文件:

<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:onClick="readPrivate"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="读私有文件" />    <Button        android:onClick="writePrivate"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="写私有文件" />    <Button        android:onClick="readdefault"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="读默认文件" />    <Button        android:onClick="writedefault"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="写默认文件" />     <Button        android:onClick="readreadable"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="读可读文件" />    <Button        android:onClick="writereadable"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="写可读文件" /></LinearLayout>

类文件:

package com.example.other;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void readPrivate(View view) {try {File file = new File("/data/data/com.example.shenchao2/files/private.txt");FileInputStream fis = new FileInputStream(file);BufferedReader br = new BufferedReader(new InputStreamReader(fis));String result = br.readLine();Toast.makeText(this, result, Toast.LENGTH_LONG).show();} catch (Exception e) {e.printStackTrace();Toast.makeText(this, "读取私有文件失败!", Toast.LENGTH_LONG).show();}}public void writePrivate(View view) {try {File file = new File("/data/data/com.example.henchao2/files/private.txt");FileOutputStream fos = new FileOutputStream(file);fos.write("haha".getBytes());fos.close();} catch (Exception e) {e.printStackTrace();Toast.makeText(this, "写私有文件失败!", Toast.LENGTH_LONG).show();}}public void readdefault(View view) {try {File file = new File("/data/data/com.example.henchao2/files/info.txt");FileInputStream fis = new FileInputStream(file);BufferedReader br = new BufferedReader(new InputStreamReader(fis));String result = br.readLine();Toast.makeText(this, result, Toast.LENGTH_LONG).show();} catch (Exception e) {e.printStackTrace();Toast.makeText(this, "读取默认文件失败!", Toast.LENGTH_LONG).show();}}public void writedefault(View view) {try {File file = new File("/data/data/com.example.shenchao2/files/info.txt");FileOutputStream fos = new FileOutputStream(file);fos.write("haha".getBytes());fos.close();} catch (Exception e) {e.printStackTrace();Toast.makeText(this, "写默认文件失败!", Toast.LENGTH_LONG).show();}}public void readreadable(View view) {try {File file = new File("/data/data/com.example.shenchao2/files/read.txt");FileInputStream fis = new FileInputStream(file);BufferedReader br = new BufferedReader(new InputStreamReader(fis));String result = br.readLine();Toast.makeText(this, result, Toast.LENGTH_LONG).show();} catch (Exception e) {e.printStackTrace();Toast.makeText(this, "读取默认文件失败!", Toast.LENGTH_LONG).show();}}public void writereadable(View view) {try {File file = new File("/data/data/com.example.shenchao2/files/read.txt");FileOutputStream fos = new FileOutputStream(file);fos.write("haha".getBytes());fos.close();} catch (Exception e) {e.printStackTrace();Toast.makeText(this, "写默认文件失败!", Toast.LENGTH_LONG).show();}}}
   查看权限:在cmd中写入:

   adb shell    cd/data/data/   ls -l  显示权限   

    cd com.example.shenchao2    cd files(查看当前的权限)    ls -l   chmod   666 Private.txt(修改文件的权限)   ls -l(权限变了)

   

资料图片:

0 0
原创粉丝点击