Android学习11
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的更多相关文章
- android学习11——Handler,Looper,MessageQueue工作原理
Message是Handler接收和处理的消息对象. 每个线程只能拥有一个Looper.它的loop方法读取MessageQueue中的消息,读到消息之后就把消息交给发送该消息的Handler进行处理 ...
- Android学习系列(11)--App列表之拖拽ListView(下)
接着上篇Android学习系列(10)--App列表之拖拽ListView(上)我们继续实现ListView的拖拽效果. 7.重写onTouchEvent()方法. 在这个方法中我们主要是处理 ...
- 【转】Pro Android学习笔记(二三):用户界面和控制(11):其他控件
目录(?)[-] Chronometer计时器控件 倒计时CountDownTimer Switch控件 Space控件 其他控件 Android提供了很多控件,基本上都是view的扩展. Chron ...
- Android学习路线总结,绝对干货
title: Android学习路线总结,绝对干货 tags: Android学习路线,Android学习资料,怎么学习android grammar_cjkRuby: true --- 一.前言 不 ...
- Android学习——windows下搭建Cygwin环境
在上一篇博文<Android学习——windows下搭建NDK_r9环境>中,我们详细的讲解了在windows下进行Android NDK开发环境的配置,我们也讲到了在NDk r7以后,我 ...
- [转]Android 学习资料分享(2015 版)
转 Android 学习资料分享(2015 版) 原文地址:http://www.jianshu.com/p/874ff12a4c01 目录[-] 我是如何自学Android,资料分享(2015 版) ...
- Android学习系列(37)--App调试内存泄露之Context篇(下)
接着<Android学习系列(36)--App调试内存泄露之Context篇(上)>继续分析. 5. AsyncTask对象 我N年前去盛大面过一次试,当时面试官极力推荐我使用AsyncT ...
- Android – 学习操作NFC – 2
在<Android – 学习操作NFC – 1>说明了Android在处理NFC tag的机制.tag dispatch system的运作流程,以及三种ACTION_NDEF_DISCO ...
- android学习系列:jercy——AI3 的博客
[android学习之十七]——特色功能2:桌面组件(快捷方式,实时文件夹) 二.桌面组件 1.快捷方式 Android手机上得快捷方式的意思可以以我们实际PC机器上程序的快捷方式来理解.而andro ...
随机推荐
- 《深入理解java虚拟机》读书笔记三——第四章
第四章 虚拟机性能监控与故障处理工具 1.JDK命令行工具 jps命令: 作用:列出正在运行的虚拟机进程. 格式:jps [option] [hostid] 选项:-q 只输出LVMID(Local ...
- [POI2006] SZK-Schools - 费用流
差不多就是个二分图带权匹配?(我还是敲费用流吧) 每个点向着自己能到的学校连边,费用按题意设定 跑最小费用最大流即可 #include <bits/stdc++.h> using name ...
- springBoot 2.1.5 pom 文件 unknown 错误
插件不兼容 pom文件添加 <properties> <maven-jar-plugin.version>2.6</maven-jar-plugin.version ...
- 右键添加IDEA打开
在安装IntelliJ IDEA时可能没有选择文件用idea打开的选项,现在有这个需求. 下面就演示一下,如何添加文件右键用idea打开! 1. 打开注册表 win+R键输入regedit 2. 找到 ...
- c语言 printf格式化输出
#include <iostream> #include<stdio.h> #include <cstring> using namespace std; int ...
- [JZOJ5060] 公路建设
题目描述 在Byteland一共有n个城市,编号依次为1到n,它们之间计划修建m条双向道路,其中修建第i条道路的费用为ci. Byteasar作为Byteland公路建设项目的总工程师,他决定选定一个 ...
- react中用swiper实现大图功能
1.引入Swiper(用的是4.5.0版本) import Swiper from 'swiper'; //引入样式,还可以加上自己的样式 import '../../s ...
- AcWing 272. 最长公共上升子序列
#include<iostream> using namespace std ; ; int n; int a[N]; int b[N]; int f[N][N]; //f[i][j] / ...
- macaron 根目录默认为templates文件夹,所以如果启动目录同目录下有templates目录,要给它指定另一个文件夹
m *macaron.Macaron //随便指向一个目录,因为web没用到模板,不能使用默认值templates,因为这个目录被其他模板占用了m.Use(macaron.Renderer(macar ...
- 题解【BZOJ4472】[JSOI2015]salesman
题面 树形\(\text{DP}\)与贪心的结合. 首先考虑树形\(\text{DP}\). 设\(dp_i\)表示从\(i\)出发,访问\(i\)的子树,并且最后回到\(i\)能获得的最大收益. 转 ...