zoukankan      html  css  js  c++  java
  • 004_02文件读写模式

    SharedPreferences的四种操作模式:

      Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容

      Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.

      Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.

     1 package com.example.readfile;
     2 
     3 import java.io.FileNotFoundException;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 
     7 import android.app.Activity;
     8 import android.content.Context;
     9 import android.os.Bundle;
    10 import android.view.Menu;
    11 import android.view.MenuItem;
    12 import android.view.View;
    13 
    14 public class MainActivity extends Activity {
    15 
    16     @Override
    17     protected void onCreate(Bundle savedInstanceState) {
    18         super.onCreate(savedInstanceState);
    19         setContentView(R.layout.activity_main);
    20     }
    21     
    22     private void creatFile(){
    23         try{
    24             FileOutputStream fos = this.openFileOutput("woodrow.txt",Context.MODE_PRIVATE);
    25             fos.write("牵黄擎苍".getBytes());
    26             fos.close();
    27         }catch(FileNotFoundException e){
    28             e.printStackTrace();
    29         }catch(IOException e){
    30             e.printStackTrace();
    31         }
    32     }
    33     
    34     public void private_mode(View v){
    35         FileOutputStream fos;
    36         try {
    37             fos = this.openFileOutput("woodrow1.txt", Context.MODE_PRIVATE);
    38             fos.write("牵黄擎苍".getBytes());
    39             fos.close();
    40         } catch (FileNotFoundException e) {
    41             e.printStackTrace();
    42         } catch (IOException e) {
    43             e.printStackTrace();
    44         }
    45         
    46     }
    47     
    48     public void world_readable_mode(View v){
    49         try {
    50             FileOutputStream outStream = this.openFileOutput("woodrow2.txt", Context.MODE_WORLD_READABLE);
    51              outStream.write("牵黄擎苍".getBytes());
    52              outStream.close();
    53         } catch (FileNotFoundException e) {
    54             // TODO Auto-generated catch block
    55             e.printStackTrace();
    56         } catch (IOException e) {
    57             // TODO Auto-generated catch block
    58             e.printStackTrace();
    59         }
    60         
    61     }
    62     
    63     public void world_writeable_mode(View v){
    64         try {
    65             FileOutputStream outStream = this.openFileOutput("woodrow3.txt", Context.MODE_WORLD_WRITEABLE);
    66              outStream.write("牵黄擎苍".getBytes());
    67              outStream.close();
    68         } catch (FileNotFoundException e) {
    69             // TODO Auto-generated catch block
    70             e.printStackTrace();
    71         } catch (IOException e) {
    72             // TODO Auto-generated catch block
    73             e.printStackTrace();
    74         }
    75     }
    76     public void world_writeandread_mode(View v){
    77         try {
    78             FileOutputStream outStream = this.openFileOutput("woodrow4.txt", 
    79                     Context.MODE_WORLD_WRITEABLE|Context.MODE_WORLD_READABLE);
    80              outStream.write("王道论坛".getBytes());
    81              outStream.close();
    82         } catch (FileNotFoundException e) {
    83             // TODO Auto-generated catch block
    84             e.printStackTrace();
    85         } catch (IOException e) {
    86             // TODO Auto-generated catch block
    87             e.printStackTrace();
    88         }
    89     }
    90 }
     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context="com.example.readfile.MainActivity" 
    10     android:orientation="vertical">
    11 
    12     <TextView
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:text="@string/hello_world" />
    16     
    17      <Button 
    18          android:onClick="private_mode"
    19           android:layout_width="wrap_content"
    20         android:layout_height="wrap_content"
    21         android:text="private_mode"/>
    22      <Button 
    23          android:onClick="world_readable_mode"
    24           android:layout_width="wrap_content"
    25         android:layout_height="wrap_content"
    26         android:text="world_readable_mode"/>
    27           <Button 
    28          android:onClick="world_writeable_mode"
    29           android:layout_width="wrap_content"
    30         android:layout_height="wrap_content"
    31         android:text="world_writeable_mode"/>
    32       <Button 
    33          android:onClick="world_writeandread_mode"
    34           android:layout_width="wrap_content"
    35         android:layout_height="wrap_content"
    36         android:text="world_writeandread_mode"/> 
    37 
    38 </LinearLayout>

    效果图:

    第一位表示文件类型。d是目录文件,l是链接文件,-是普通文件,p是管道
    第2-4位表示这个文件的属主拥有的权限,r是读,w是写,x是执行。
    第5-7位表示和这个文件属主所在同一个组的用户所具有的权限。
    第8-10位表示其他用户所具有的权限。
    如:
    drwxr-xr-x user1 group1 filename

    表示filename是个目录,user1拥有读写执行的权限,和user1所在同一个group1组里的用户拥有只读和执行权限,剩下其他用户拥有只读和执行权限。
    物随心转,境由心造,一切烦恼皆由心生。
  • 相关阅读:
    Java程序:从命令行接收多个数字,求和并输出结果
    大道至简读后感
    大道至简第一章读后感Java伪代码
    Creating a SharePoint BCS .NET Connectivity Assembly to Crawl RSS Data in Visual Studio 2010
    声明式验证超时问题
    Error message when you try to modify or to delete an alternate access mapping in Windows SharePoint Services 3.0: "An update conflict has occurred, and you must re-try this action"
    Upgrading or Redeploying SharePoint 2010 Workflows
    Upgrade custom workflow in SharePoint
    SharePoint 2013中Office Web Apps的一次排错
    How to upgrade workflow assembly in MOSS 2007
  • 原文地址:https://www.cnblogs.com/woodrow2015/p/4515696.html
Copyright © 2011-2022 走看看