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. layedit不可编辑,按钮不能使用

    $("#LAY_layedit_1").contents().find("body[contenteditable]").prop("contente ...

  2. Xlrd模块读取Excel文件数据

    Xlrd模块使用 excel文件样例:

  3. python接口自动化之发送get(三)

    1.安装requests requests是python的第三方库,需要进行安装.安装之前最好先关闭fiddler cmd(win+R快捷键)输入:pip install requests 其他命令: ...

  4. 记录 shell学习过程(9)正则表达式 转自树明聊运维

    正则表达式 正则表达式介绍 特殊字符 POSIX特殊字符 一.正则表达式介绍 正则表达式是一种文本模式匹配,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符") ...

  5. paper: VG -- re-read

    重点:  1.The constructed graph inherits several properties of the series in its structure. periodic se ...

  6. (转)DNS使用的是TCP协议还是UDP协议

    转自:DNS使用的是TCP协议还是UDP协议 DNS同时占用UDP和TCP端口53是公认的,这种单个应用协议同时使用两种传输协议的情况在TCP/IP栈也算是个另类.但很少有人知道DNS分别在什么情况下 ...

  7. [Luogu]三步必杀

    Description Luogu4231 Solution 我最近到底怎么了,这种题都做不出来了,一看题第一反应李超线段树(虽然不会),觉得不可做,看一眼题解才发现这个题可以差分,然后差分还打错了好 ...

  8. python3练习100题——009

    今天的题目好像有点水... 原题链接:http://www.runoob.com/python/python-exercise-example9.html 题目:暂停一秒输出. 我的代码: impor ...

  9. Java之字符串替换replace()

    replace(char oldChar, char newChar)返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的 import java.uti ...

  10. 【译】PHP 内核 — 字符串管理

    [译]PHP 内核 - 字符串管理 (Strings management: zend_string 译文) 原文地址:http://www.phpinternalsbook.com/php7/int ...