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

    一.特点

    (1)存储的是任意类型的文件

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

    (3)存放的目录:/date/date/包名/files/

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

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

    二.API

    (1)FileOutputStream  文件输出流

    ①openFileOutput(文件名,操作模式)

    mode  操作模式:MODE_PRIVATE不能被别的应用访问,覆盖模式

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

    ②close()  关闭输出流

    (2)PrintStream  打印流

    ①new PrintStream(输出流)

    ②print(String)  打印字符串

    ③println(String)打印字符串并自动换行

    ④close()  关闭打印流

    (3)FileInputStream  文件输入流

    ①openFileInput(文件名)

    ②close()  关闭输入流

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

    (4)File  文件或目录

    ①getFileDir()返回代表内部存储目录的File实例

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

      

      

  • 相关阅读:
    .net core读取appsettings.config中文乱码问题
    vs2017错误:当前页面的脚本发生错误
    VS Code中无法识别npm命令
    Visual Studio报错/plugin.vs.js,行:1074,错误:缺少标识符、字符串或数字
    记录一次在生成数据库服务器上出现The timeout period elapsed prior to completion of the operation or the server is not responding.和Exception has been thrown by the target of an invocation的解决办法
    Java集合框架
    java hash表
    Java Dictionary 类存储键值
    java数据结构 栈stack
    java封装
  • 原文地址:https://www.cnblogs.com/cycanfly/p/5531620.html
Copyright © 2011-2022 走看看