Android 数据库文件存取至储存卡的方法
作者:gisoracle 发布时间:2023-05-08 19:12:14
标签:android,数据,文件,存取卡
废话不多说了,直接给大家贴代码了,具体代码如下
<?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" >
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存数据(File)" />
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取数据(File)" />
</LinearLayout>
package com.example.yanlei.wifi;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class MainActivity extends AppCompatActivity {
private Button btnSave=null;
private Button btnRead=null;
private File file=null;
private static final String FILENAME="data.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSave=(Button)super.findViewById(R.id.save);
btnRead=(Button)super.findViewById(R.id.read);
btnSave.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
PrintStream ps=null;
//判断外部存储卡是否存在
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(getApplicationContext(), "读取失败,SD存储卡不存在!", Toast.LENGTH_LONG).show();
return;
}
//初始化File
String path=Environment.getExternalStorageDirectory().toString()
+File.separator
+"genwoxue"
+File.separator
+FILENAME;
file=new File(path);
//如果当前文件的父文件夹不存在,则创建genwoxue文件夹
if(!file.getParentFile().exists())
file.getParentFile().mkdirs();
//写文件
try {
ps = new PrintStream(new FileOutputStream(file));
ps.println("跟我学网址:www.genwoxue.com");
ps.println("");
ps.println("电子邮件:hello@genwoxue.com");
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
ps.close();
}
Toast.makeText(getApplicationContext(), "保存成功!", Toast.LENGTH_LONG).show();
}
});
btnRead.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
StringBuffer info=new StringBuffer();
//判断外部存储卡是否存在
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(getApplicationContext(), "读取失败,SD存储卡不存在!", Toast.LENGTH_LONG).show();
return;
}
//初始化File
String path=Environment.getExternalStorageDirectory().toString()
+File.separator
+"genwoxue"
+File.separator
+FILENAME;
file=new File(path);
if(!file.exists()){
Toast.makeText(getApplicationContext(), "文件不存在!", Toast.LENGTH_LONG).show();
return;
}
//读取文件内容
Scanner scan=null;
try {
scan=new Scanner(new FileInputStream(file));
while(scan.hasNext()){
info.append(scan.next()).append("☆☆☆\n");
}
Toast.makeText(getApplicationContext(), info.toString(), Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
scan.close();
}
}
});
}
}
权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yanlei.wifi" >
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
以上所述是小编给大家介绍的Android 数据库文件存取至储存卡的方法,希望对大家有所帮助,本文写的不好还请各位大侠见谅!


猜你喜欢
- C#时间/日期格式大全,C#时间/日期函数大全有时候我们要对时间进行转换,达到不同的显示效果默认格式为:2005-6-6 14:33:34如
- 序言:此前,我们主要通过XML来书写SQL和填补对象映射关系。在SpringBoot中我们可以通过注解来快速编写SQL并实现数据访问。(仅需
- 最近项目里涉及到自定义View的东西还是挺多的,所以打算在自定义View上多花点时间,也顺便分享给大家。先总结下自定义View的步骤:1、自
- @Lazy用于指定该Bean是否取消预初始化。主要用于修饰Spring Bean类,用于指定该Bean的预初始化行为,使用该Annotati
- 部署到webapps目录启动本文使用的Spring版本为Spring6,SpringBoot版本为3,JDK为17,可能会和之前有细微不同,
- 我们都知道Android Studio用起来很棒,其中布局预览更棒。我们在调UI的时候基本是需要实时预览来看效果的,在Android Stu
- 本文实例讲述了Android AutoCompleteTextView连接数据库自动提示的方法。分享给大家供大家参考,具体如下:这个简单例子
- OpenFeign介绍一开始,我们使用原生的 DiscoveryClient 发现服务和使用RestTemplate进行服务间调用,然后我们
- 前言提问:springboot项目,开发环境、测试环境和生产环境配置文件如何分开表示?答:多profile文件方式1、多环境配置(profi
- mybatis 传入null值解决前端传入两个值,如果其中一个为null时,很多时候我们都很困惑,明明传入的是null,为啥mybatis
- 1. 可变参数在JDK1.5之后,如果我们定义一个方法需要接受多个参数,并且多个参数类型一致,我们可以对其简化.格式:修饰符 返回值类型 方
- 本文实例讲述了Java数组的定义、初始化、及二维数组用法。分享给大家供大家参考,具体如下:数组的定义1.数组是有序数据的集合,数组中的每个元
- C#调用dll报错:无法加载dll,找不到指定模块最近在做一个swmm模型的项目,在swmm源码上进行改写了两个函数,结果调用的时候就报错了
- AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合。其实AsyncTask并不是那么好,甚至有些糟糕。本文
- 本文实例为大家分享了Android滑动组件悬浮固定在顶部效果的具体代码,供大家参考,具体内容如下要想实现的效果是如下:场景:有些时候是内容中
- 简介:Android Studio是Android的官方IDE。它是专为Android而打造,可以加快您的开发速度,帮助您为每款Androi
- 目录通过Resource接口手动加载通过@Value自动转换通过ResourceLoader加载使用ResourceUtils加载资源读取资
- 在每一个窗体生成的时候,都会针对于当前的窗体定义InitializeComponent()方法,该方法实际上是由系统生成的对于窗体界面的定义
- 如下所示:private void readImage(String filename) throws FileNotFound
- 开发环境JDK1.8 eclipse struts2-2.3.31 1.创建web项目 2.导入struts2核心jar包 3.更改web.