建立两个项目,一个是写数据_相关,一个是读数据_相关数据 ,
写数据_相关 作用:建立,私有文件,可读文件,可读文件,可读可写文件
读数据_相关数据作用:对上面的项目的文件进行读写操作,看看是否能成功
写数据_相关:
package com.sqf.writedata; import java.io.FileNotFoundException; import java.io.FileOutputStream; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //写数据 //私有文件 writeToLocal("private.txt",Context.MODE_PRIVATE); //可读文件 writeToLocal("readable.txt", Context.MODE_WORLD_READABLE); //可写文件 writeToLocal("writeable.txt", Context.MODE_WORLD_WRITEABLE); //可读可写文件. writeToLocal("readable_writeable.txt", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE); } private void writeToLocal(String fileName,int mode){ try { // /data/data/包名/files/ //第一个参数是data/data/包名/files/下的文件名,di二是参数是权限 FileOutputStream fos = openFileOutput(fileName, mode); fos.write(("第一个程序写的数据"+fileName).getBytes()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }
读数据_相关数据:
package com.sqf.write; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { //为什么不能动态获取? 因为这是另一个项目的地址 private String basicPath = "/data/data/com.sqf.writedata/files/"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn_read_private).setOnClickListener(this); findViewById(R.id.btn_write_private).setOnClickListener(this); findViewById(R.id.btn_read_readable).setOnClickListener(this); findViewById(R.id.btn_write_readable).setOnClickListener(this); findViewById(R.id.btn_read_writeable).setOnClickListener(this); findViewById(R.id.btn_write_writeable).setOnClickListener(this); findViewById(R.id.btn_read_readable_writeable).setOnClickListener(this); findViewById(R.id.btn_write_readable_writeable).setOnClickListener(this); } /** * 哪一个空间被点击,V对象就代表被点击的对象 * */ @Override public void onClick(View v) { switch(v.getId()){ case R.id.btn_read_private: readFile("private.txt"); break; case R.id.btn_write_private: writeFile("private.txt"); break; case R.id.btn_read_readable: readFile("readable.txt"); break; case R.id.btn_write_readable: writeFile("readable.txt"); break; case R.id.btn_read_writeable: readFile("writeable.txt"); break; case R.id.btn_write_writeable: writeFile("writeable.txt"); break; case R.id.btn_read_readable_writeable: readFile("readable_writeable.txt"); break; case R.id.btn_write_readable_writeable: writeFile("readable_writeable.txt"); break; } } private void readFile(String fileName){ try { String path = basicPath + fileName; BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path))); String text = reader.readLine(); reader.close(); Toast.makeText(this, "读取成功", 0).show(); } catch (Exception e) { Toast.makeText(this, "读取失败"+fileName, 0).show(); e.printStackTrace(); } } private void writeFile(String fileName){ try { String path = basicPath + fileName; FileOutputStream fos = new FileOutputStream(path); fos.write("哈哈,被别人黑了".getBytes()); fos.flush(); fos.close(); Toast.makeText(this, "写入成功"+fileName, 0).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "写入失败", 0).show(); } } }
<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" > <Button android:id="@+id/btn_read_private" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读私有数据" /> <Button android:id="@+id/btn_write_private" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写私有数据" /> <Button android:id="@+id/btn_read_readable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读可读数据" /> <Button android:id="@+id/btn_write_readable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写可读数据" /> <Button android:id="@+id/btn_read_writeable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读可写数据" /> <Button android:id="@+id/btn_write_writeable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写可写数据" /> <Button android:id="@+id/btn_read_readable_writeable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读可读可写数据" /> <Button android:id="@+id/btn_write_readable_writeable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写可读可写数据" /> </LinearLayout>
重点:
利用chmod去修改权限
#ls -l 显示权限
666---把2--10看成二进制,r,w代表为1, 666代表2-4位,5-7位,8-10位都有读写权限