zoukankan      html  css  js  c++  java
  • 数据存储——手机内部文件存储

    一.特点

       1.存储的是任意类型的文件

       2.使用IO输入输出流操作文件

       3.存放的目录:/data/data/包名/files/

       4.可以设置不被其他应用操作

       5.应用卸载之后,数据同时被删除

    二.API

       1.FileOutputStream  文件输出流

         1-openFileOutput(文件名,操作模式)

           mode  操作模式

            1>MODE_PRIVATE,不能被别的应用访问,覆盖模式

            2>MODE_APPEND,不能被别的应用访问,追加模式

         2-close( )关闭输出流

       2.PrintStream  打印流

         1-new  PrintStream(输出流)

         2-print(String) 打印字符串

         3-printIn(String)  打印字符串并自动换行

         4-close( )  关闭打印流

       3.FileInputStream  文件输入流

         1-openFileInput(文件名)

         2-close( )关闭输入流

         3-read(byte[ ]),把读到的字节保存到byte[ ]里,并返回读到的数据长度。

       4.File  文件或目录

         1-getFilesDir( )   返回代表内部存储目录的File实例

         2-getAbsolutePath( ) 返回绝对路径 

    代码展示:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.hanqi.testapp3.MainActivity"
    11     android:orientation="vertical">
    12 
    13     <TextView
    14         android:layout_width="wrap_content"
    15         android:layout_height="wrap_content"
    16         android:text="Hello World!"
    17         android:id="@+id/tv_1"/>
    18 
    19 
    20     <EditText
    21         android:layout_width="match_parent"
    22         android:layout_height="wrap_content"
    23         android:hint="输入..."
    24         android:id="@+id/et_1"/>
    25 
    26     <Button
    27         android:layout_width="match_parent"
    28         android:layout_height="wrap_content"
    29         android:text="写内容文件"
    30         android:onClick="bt2_OnClick"/>
    31 
    32     <Button
    33         android:layout_width="match_parent"
    34         android:layout_height="wrap_content"
    35         android:text="读内容文件"
    36         android:onClick="bt3_OnClick"/>
    37 </LinearLayout>
    .xml
      1 package com.hanqi.testapp3;
      2 
      3 import android.content.SharedPreferences;
      4 import android.support.v7.app.AppCompatActivity;
      5 import android.os.Bundle;
      6 import android.view.View;
      7 import android.widget.EditText;
      8 import android.widget.TextView;
      9 import android.widget.Toast;
     10 
     11 import java.io.File;
     12 import java.io.FileInputStream;
     13 import java.io.FileOutputStream;
     14 import java.io.PrintStream;
     15 
     16 
     17 public class MainActivity extends AppCompatActivity {
     18 
     19     TextView tv_1;
     20     EditText et_1;
     21 
     22     @Override
     23     protected void onCreate(Bundle savedInstanceState) {
     24         super.onCreate(savedInstanceState);
     25         setContentView(R.layout.activity_main);
     26 
     27         et_1=(EditText)findViewById(R.id.et_1);
     28         tv_1=(TextView)findViewById(R.id.tv_1);
     29 
     30     }
     31 
     32 
     33     //写内容文件
     34     public void bt2_OnClick(View v)
     35     {
     36         //从内存里写入文件
     37 
     38         //1.得到内部的存储目录
     39         try {
     40 
     41             File file =getFilesDir();
     42 
     43             String path=file.getAbsolutePath();
     44 
     45             Toast.makeText(MainActivity.this, "path"+path, Toast.LENGTH_SHORT).show();
     46 
     47             //2.用输出流写入文件
     48             FileOutputStream fos=openFileOutput("test.txt",MODE_APPEND);
     49 
     50             //写入文件内容
     51             PrintStream ps=new PrintStream(fos);
     52 
     53             String str=et_1.getText().toString();
     54 
     55             ps.println(str);
     56             //ps.println("自动换行");
     57 
     58             ps.close();
     59 
     60             fos.close();
     61             Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
     62 
     63         }
     64         catch (Exception e)
     65         {
     66 
     67             Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
     68         }
     69 
     70     }
     71 
     72     //
     73     public void bt3_OnClick(View v)
     74     {
     75         //输入流
     76         try {
     77 
     78             FileInputStream fis=openFileInput("test.txt");
     79 
     80             //1 定义byte[]
     81             byte [] b=new byte[1024];
     82             int i=0;//读到的数据长度
     83 
     84             String str1="";
     85 
     86             //2循环读
     87             while ((i=fis.read(b))>0)
     88             {
     89                 String str=new String(b,0,i);
     90 
     91                 str1+=str;
     92             }
     93 
     94             fis.close();
     95 
     96             tv_1.setText(str1);
     97         }
     98         catch (Exception e)
     99         {
    100 
    101         }
    102 
    103 
    104     }
    105 }
    .java

  • 相关阅读:
    应用程序中的服务器错误,怎么解决
    日期格式
    怎样破解网页禁用鼠标右键
    web.config中配置页面出错后跳转指定错误页面
    无法复制文件
    jquery 上传图片即时预览功能
    jquery若干问题
    web.xml 中的listener、 filter、servlet 加载顺序及其详解
    使用jquery 对 radio 和 select 的各种操作.
    零散技术整理
  • 原文地址:https://www.cnblogs.com/arxk/p/5531691.html
Copyright © 2011-2022 走看看