Android存储概念

File内部存储

通过file=openFileOutput()获得,将数据存储在data/data/+包名+files下面。

layout布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp"> <EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入内容"/> <Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="保存"
android:textSize="20sp"/> <Button
android:id="@+id/btn_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="显示"
android:textSize="20sp"/> <TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/> </LinearLayout>

activity_file

Activity:

package com.example.helloworld.datastorage;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; import com.example.helloworld.R; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class FileActivity extends AppCompatActivity { private EditText mEtName;
private Button mBtnSave,mBtnShow;
private TextView mTvContent;
private final String mFileName = "test.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);
mEtName = findViewById(R.id.et_name);
mBtnSave = findViewById(R.id.btn_save);
mBtnShow = findViewById(R.id.btn_show);
mTvContent = findViewById(R.id.tv_content); mBtnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save(mEtName.getText().toString());
}
});
mBtnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTvContent.setText(read());
}
});
} //存储数据
private void save(String content){
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = openFileOutput(mFileName,MODE_PRIVATE);
fileOutputStream.write(content.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fileOutputStream != null){
try{
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//读取数据
private String read(){
FileInputStream fileInputStream = null;
try {
fileInputStream = openFileInput(mFileName);
byte[] buff = new byte[1024];
//用StringBuilder来实现字符串拼接
StringBuilder sb = new StringBuilder();
int len = 0;
while((len = fileInputStream.read(buff)) > 0){
sb.append(new String(buff,0,len));
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileInputStream != null){
try{
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}

FileActivity

Android学习11的更多相关文章

  1. android学习11——Handler,Looper,MessageQueue工作原理

    Message是Handler接收和处理的消息对象. 每个线程只能拥有一个Looper.它的loop方法读取MessageQueue中的消息,读到消息之后就把消息交给发送该消息的Handler进行处理 ...

  2. Android学习系列(11)--App列表之拖拽ListView(下)

    接着上篇Android学习系列(10)--App列表之拖拽ListView(上)我们继续实现ListView的拖拽效果. 7.重写onTouchEvent()方法.     在这个方法中我们主要是处理 ...

  3. 【转】Pro Android学习笔记(二三):用户界面和控制(11):其他控件

    目录(?)[-] Chronometer计时器控件 倒计时CountDownTimer Switch控件 Space控件 其他控件 Android提供了很多控件,基本上都是view的扩展. Chron ...

  4. Android学习路线总结,绝对干货

    title: Android学习路线总结,绝对干货 tags: Android学习路线,Android学习资料,怎么学习android grammar_cjkRuby: true --- 一.前言 不 ...

  5. Android学习——windows下搭建Cygwin环境

    在上一篇博文<Android学习——windows下搭建NDK_r9环境>中,我们详细的讲解了在windows下进行Android NDK开发环境的配置,我们也讲到了在NDk r7以后,我 ...

  6. [转]Android 学习资料分享(2015 版)

    转 Android 学习资料分享(2015 版) 原文地址:http://www.jianshu.com/p/874ff12a4c01 目录[-] 我是如何自学Android,资料分享(2015 版) ...

  7. Android学习系列(37)--App调试内存泄露之Context篇(下)

    接着<Android学习系列(36)--App调试内存泄露之Context篇(上)>继续分析. 5. AsyncTask对象 我N年前去盛大面过一次试,当时面试官极力推荐我使用AsyncT ...

  8. Android – 学习操作NFC – 2

    在<Android – 学习操作NFC – 1>说明了Android在处理NFC tag的机制.tag dispatch system的运作流程,以及三种ACTION_NDEF_DISCO ...

  9. android学习系列:jercy——AI3 的博客

    [android学习之十七]——特色功能2:桌面组件(快捷方式,实时文件夹) 二.桌面组件 1.快捷方式 Android手机上得快捷方式的意思可以以我们实际PC机器上程序的快捷方式来理解.而andro ...

随机推荐

  1. try_except_finally

    blog: python中的try/except/else/finally  python文档:错误和异常 与其他语言相同,在python中,try/except语句主要是用于处理程序正常执行过程中出 ...

  2. asm相关内容想下载(包括 jar 包)

    网址:http://download.forge.ow2.org/asm/

  3. scrapy爬虫保存数据

    1.数据保存为TXT 打开Pipeline.py import codecs import os import json import pymysql class CoolscrapyPipeline ...

  4. CSS的字体样式

    CSS的字体样式 1. span标签(约定俗成:重要的东西用它括起来) 首选介绍一个约定俗成的东西:span标签.一般将想要突出的东西,比较重要的东西,用span标签括起来. 比如,”学习Java“这 ...

  5. PHP 把秒数转为时分秒格式

    PHP函数 1.gmdate $seconds = 174940;$hours = intval($seconds/); $time1 = $hours."小时".gmdate(' ...

  6. 电脑出现kernelbase.dll错误的两种解决方法

    KernelBase.dll是Windows操作系统的重要文件,它为各种应用程序提供服务.如果电脑提示kernelbase.dll错误,这该怎么处理?大家可以用电脑自带的防火墙或者是第三方软件来进行故 ...

  7. shell 预定义变量

    echo "上一次后台pid is $!"echo "当前进程pid is $$"echo "last command return code is ...

  8. 工具系列 | git checkout 可替换命令 git switch 和 git restore

    前言 git checkout 这个命令承担了太多职责,既被用来切换分支,又被用来恢复工作区文件,对用户造成了很大的认知负担. Git社区发布了Git的新版本2.23.在该版本中,有一个特性非常引人瞩 ...

  9. Safari 导航栏

    目录 引子 隐藏 Safari 导航栏 显示 Safari 导航栏 iPhone 系统占比 参考资料 引子 最近在 iPhone 的 Safari 查看 h5 页面时,发现有些平台的页面向下滚动时,顶 ...

  10. Mybatis学习笔记——输入参数parameterType、Mybatis调用存储过程

    输入参数:parameterType(两种取值符号) 1.类型为简单类型 区别:     (1) #{可以为任意值}         ${vaue}--->标识符只能是value     (2) ...