热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

数据存储——四种存储方式——存储卡的文件操作——在存储卡上读写文本文件

       

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

==========================================================================================

 

 

 

 

 

 

 

添加权限:

 


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

 

 

 

 

 

 

 

 

第一个页面布局:

<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
="5dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height
="40dp" >
<TextView
android:id="@+id/tv_name"
android:layout_width
="wrap_content"
android:layout_height
="match_parent"
android:gravity
="center"
android:text
="姓名:"
android:textColor
="@color/black"
android:textSize
="17sp" />
<EditText
android:id="@+id/et_name"
android:layout_width
="match_parent"
android:layout_height
="match_parent"
android:layout_marginBottom
="3dp"
android:layout_marginTop
="3dp"
android:layout_toRightOf
="@+id/tv_name"
android:background
="@drawable/editext_selector"
android:gravity
="left|center"
android:hint
="请输入姓名"
android:inputType
="text"
android:maxLength
="12"
android:textColor
="@color/black"
android:textSize
="17sp" />
RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height
="40dp" >
<TextView
android:id="@+id/tv_age"
android:layout_width
="wrap_content"
android:layout_height
="match_parent"
android:gravity
="center"
android:text
="年龄:"
android:textColor
="@color/black"
android:textSize
="17sp" />
<EditText
android:id="@+id/et_age"
android:layout_width
="match_parent"
android:layout_height
="match_parent"
android:layout_marginBottom
="3dp"
android:layout_marginTop
="3dp"
android:layout_toRightOf
="@+id/tv_age"
android:background
="@drawable/editext_selector"
android:gravity
="left|center"
android:hint
="请输入年龄"
android:inputType
="number"
android:maxLength
="2"
android:textColor
="@color/black"
android:textSize
="17sp" />
RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height
="40dp" >
<TextView
android:id="@+id/tv_height"
android:layout_width
="wrap_content"
android:layout_height
="match_parent"
android:gravity
="center"
android:text
="身高:"
android:textColor
="@color/black"
android:textSize
="17sp" />
<EditText
android:id="@+id/et_height"
android:layout_width
="match_parent"
android:layout_height
="match_parent"
android:layout_marginBottom
="3dp"
android:layout_marginTop
="3dp"
android:layout_toRightOf
="@+id/tv_height"
android:background
="@drawable/editext_selector"
android:gravity
="left|center"
android:hint
="请输入身高"
android:inputType
="number"
android:maxLength
="3"
android:textColor
="@color/black"
android:textSize
="17sp" />
RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height
="40dp" >
<TextView
android:id="@+id/tv_weight"
android:layout_width
="wrap_content"
android:layout_height
="match_parent"
android:gravity
="center"
android:text
="体重:"
android:textColor
="@color/black"
android:textSize
="17sp" />
<EditText
android:id="@+id/et_weight"
android:layout_width
="match_parent"
android:layout_height
="match_parent"
android:layout_marginBottom
="3dp"
android:layout_marginTop
="3dp"
android:layout_toRightOf
="@+id/tv_weight"
android:background
="@drawable/editext_selector"
android:gravity
="left|center"
android:hint
="请输入体重"
android:inputType
="numberDecimal"
android:maxLength
="5"
android:textColor
="@color/black"
android:textSize
="17sp" />
RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height
="40dp" >
<CheckBox
android:id="@+id/ck_married"
android:layout_width
="wrap_content"
android:layout_height
="match_parent"
android:gravity
="center"
android:checked
="false"
android:text
="已婚"
android:textColor
="@color/black"
android:textSize
="17sp" />
RelativeLayout>
<Button
android:id="@+id/btn_save"
android:layout_width
="match_parent"
android:layout_height
="wrap_content"
android:text
="保存文本到存储卡"
android:textColor
="@color/black"
android:textSize
="17sp" />
<TextView
android:id="@+id/tv_path"
android:layout_width
="wrap_content"
android:layout_height
="match_parent"
android:textColor
="@color/black"
android:textSize
="17sp" />
LinearLayout>

 

 

 

 

 

 

第一个页面代码:

 

package com.example.myapplication;
import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener
{
private EditText et_name;
private EditText et_age;
private EditText et_height;
private EditText et_weight;
private boolean isMarried = false;
private String[] typeArray = {"未婚", "已婚"};
private String mPath; // 私有目录路径
private TextView tv_path;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name
= findViewById(R.id.et_name);
et_age
= findViewById(R.id.et_age);
et_height
= findViewById(R.id.et_height);
et_weight
= findViewById(R.id.et_weight);
tv_path
= findViewById(R.id.tv_path);
CheckBox ck_married
= findViewById(R.id.ck_married);
ck_married.setOnCheckedChangeListener(
this);
findViewById(R.id.btn_save).setOnClickListener(
this);
// 获取当前App的私有下载目录
mPath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/";
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isMarried
= isChecked;
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_save) {
String name
= et_name.getText().toString();
String age
= et_age.getText().toString();
String height
= et_height.getText().toString();
String weight
= et_weight.getText().toString();
if (TextUtils.isEmpty(name)) {
ToastUtil.show(
this, "请先填写姓名");
return;
}
else if (TextUtils.isEmpty(age)) {
ToastUtil.show(
this, "请先填写年龄");
return;
}
else if (TextUtils.isEmpty(height)) {
ToastUtil.show(
this, "请先填写身高");
return;
}
else if (TextUtils.isEmpty(weight)) {
ToastUtil.show(
this, "请先填写体重");
return;
}
String content
= String.format(" 姓名:%s\n 年龄:%s\n 身高:%scm\n 体重:%skg\n 婚否:%s\n 注册时间:%s\n",
name, age, height, weight, typeArray[isMarried
?1:0], DateUtil.getNowDateTime("yyyy-MM-dd HH:mm:ss"));
String file_path
= mPath + DateUtil.getNowDateTime("") + ".txt";
FileUtil.saveText(file_path, content);
// 把字符串内容保存为文本文件
tv_path.setText("用户注册信息文件的保存路径为:\n" + file_path);
ToastUtil.show(
this, "数据已写入存储卡文件");
}
startActivity(
new Intent(this,MainActivity2.class));
}
}

 

 

 

 

 

 

 

 

工具类:

package com.example.myapplication;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
public class FileUtil {
// 把字符串保存到指定路径的文本文件
public static void saveText(String path, String txt) {
// 根据指定的文件路径构建文件输出流对象
try (FileOutputStream fos = new FileOutputStream(path)) {
fos.write(txt.getBytes());
// 把字符串写入文件输出流
} catch (Exception e) {
e.printStackTrace();
}
}
// 从指定路径的文本文件中读取内容字符串
public static String openText(String path) {
String readStr
= "";
// 根据指定的文件路径构建文件输入流对象
try (FileInputStream fis = new FileInputStream(path)) {
byte[] b = new byte[fis.available()];
fis.read(b);
// 从文件输入流读取字节数组
readStr = new String(b); // 把字节数组转换为字符串
} catch (Exception e) {
e.printStackTrace();
}
return readStr; // 返回文本文件中的文本字符串
}
// 把位图数据保存到指定路径的图片文件
public static void saveImage(String path, Bitmap bitmap) {
// 根据指定的文件路径构建文件输出流对象
try (FileOutputStream fos = new FileOutputStream(path)) {
// 把位图数据压缩到文件输出流中
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
}
catch (Exception e) {
e.printStackTrace();
}
}
// 从指定路径的图片文件中读取位图数据
public static Bitmap openImage(String path) {
Bitmap bitmap
= null; // 声明一个位图对象
// 根据指定的文件路径构建文件输入流对象
try (FileInputStream fis = new FileInputStream(path)) {
bitmap
= BitmapFactory.decodeStream(fis); // 从文件输入流中解码位图数据
} catch (Exception e) {
e.printStackTrace();
}
return bitmap; // 返回图片文件中的位图数据
}
public static List getFileList(String path, String[] extendArray) {
List
displayedCOntent= new ArrayList();
File[] files
= null;
File directory
= new File(path);
if (extendArray != null && extendArray.length > 0) {
FilenameFilter fileFilter
= getTypeFilter(extendArray);
files
= directory.listFiles(fileFilter);
}
else {
files
= directory.listFiles();
}
if (files != null) {
for (File f : files) {
if (!f.isDirectory() && !f.isHidden()) {
displayedContent.add(f);
}
}
}
// 按照最后修改时间排序
Collections.sort(displayedContent, new Comparator() {
@Override
public int compare(File o1, File o2) {
return (o1.lastModified() > o2.lastModified()) ? -1 : 1;
}
});
return displayedContent;
}
public static FilenameFilter getTypeFilter(String[] extendArray) {
final ArrayList fileExtensiOns= new ArrayList();
for (int i = 0; i ) {
fileExtensions.add(extendArray[i]);
}
FilenameFilter fileNameFilter = new FilenameFilter() {
@Override
public boolean accept(File directory, String fileName) {
boolean matched = false;
File f
= new File(String.format("%s/%s",
directory.getAbsolutePath(), fileName));
matched
= f.isDirectory();
if (!matched) {
for (String s : fileExtensions) {
s
= String.format(".{0,}\\%s$", s);
s
= s.toUpperCase(Locale.getDefault());
fileName
= fileName.toUpperCase(Locale.getDefault());
matched
= fileName.matches(s);
if (matched) {
break;
}
}
}
return matched;
}
};
return fileNameFilter;
}
}

 

 

 

 

 

 

package com.example.myapplication;
import android.content.Context;
import android.widget.Toast;
public class ToastUtil
{
public static void show(Context ctx, String desc)
{
Toast.makeText(ctx, desc, Toast.LENGTH_SHORT).show();
}
}

 

 

 

 

 

 

 

 

 

 

 

 

第二个页面布局:

<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
="5dp" >
<Button
android:id="@+id/btn_delete"
android:layout_width
="match_parent"
android:layout_height
="wrap_content"
android:text
="删除所有文本文件"
android:textColor
="@color/black"
android:textSize
="17sp" />
<TextView
android:id="@+id/tv_content"
android:layout_width
="match_parent"
android:layout_height
="wrap_content"
android:textColor
="@color/black"
android:textSize
="17sp" />
LinearLayout>

 

 

 

 

 

 

 

第二个页面代码:

package com.example.myapplication;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener
{
private final static String TAG = "FileReadActivity";
private TextView tv_content;
private String mPath; // 私有目录路径
private List mFilelist = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv_content
= findViewById(R.id.tv_content);
findViewById(R.id.btn_delete).setOnClickListener(
this);
// 获取当前App的私有下载目录
mPath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/";
showFileContent();
// 显示最新的文本文件内容
}
// 显示最新的文本文件内容
private void showFileContent() {
// 获得指定目录下面的所有文本文件
mFilelist = FileUtil.getFileList(mPath, new String[]{".txt"});
if (mFilelist.size() > 0) {
// 打开并显示选中的文本文件内容
String file_path = mFilelist.get(0).getAbsolutePath();
String content
= FileUtil.openText(file_path);
String desc
= String.format("找到最新的文本文件,路径为%s,内容如下:\n%s",
file_path, content);
tv_content.setText(desc);
}
else {
tv_content.setText(
"私有目录下未找到任何文本文件");
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_delete) {
for (int i = 0; i ) {
String file_path = mFilelist.get(i).getAbsolutePath();
File f
= new File(file_path);
if (!f.delete()) {
Log.d(TAG,
"file_path=" + file_path + ", delete failed");
}
}
ToastUtil.show(
this, "已删除私有目录下的所有文本文件");
}
}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



推荐阅读
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • fileuploadJS@sectionscripts{<scriptsrc~Contentjsfileuploadvendorjquery.ui.widget.js ... [详细]
author-avatar
housyou晶
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有