Android 数据库文件存取至储存卡的方法
更新时间:2016年03月06日 16:18:53 作者:gisoracle
这篇文章主要介绍了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 数据库文件存取至储存卡的方法,希望对大家有所帮助,本文写的不好还请各位大侠见谅!
您可能感兴趣的文章:
- Android编程中FileOutputStream与openFileOutput()的区别分析
- Android 数据存储之 FileInputStream 工具类及FileInputStream类的使用
- android实现Uri获取真实路径转换成File的方法
- Android学习笔记-保存文件(Saving Files)
- Android类FileDownloadList分析
- android开发教程之获取power_profile.xml文件的方法(android运行时能耗值)
- WAC启动Android模拟器 transfer error: Read-only file system错误解决方法
- 实例详解Android文件存储数据方式
- Android使用文件进行数据存储的方法
- Android采用File形式保存与读取数据的方法
相关文章
自定义TextView跑马灯效果可控制启动/停止/速度/焦点
Android自带的跑马灯效果不太好控制,不能控制速度,不能即时停止和启动,而且还受焦点的影响不已,由于项目需求需所以自己写了一个自定义的TextView,感兴趣的朋友可以了解下2013-01-01
详解如何使用VisualStudio高效开发调试AndroidNDK
这篇文章主要介绍了详解如何使用VisualStudio高效开发调试AndroidNDK,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-12-12
Android studio 3.0 查看手机文件系统的方法(超简单)
本文给大家分享Android studio更新到3.0版本之后,查看手机文件系统的方法,需要的朋友参考下吧2017-11-11
Android使用Jetpack WindowManager开发可折叠设备(过程分享)
这篇文章主要介绍了Android使用Jetpack WindowManager开发可折叠设备,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧2023-11-11
Android编程实现自定义Dialog的大小自动控制方法示例
这篇文章主要介绍了Android编程实现自定义Dialog的大小自动控制方法,结合实例形式分析了Android自定义Dialog对话框的属性操作技巧与大小动态控制实现方法,需要的朋友可以参考下2017-09-09
AndroidStudio接入Unity工程并实现相互跳转的示例代码
这篇文章主要介绍了AndroidStudio接入Unity工程并实现相互跳转,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-12-12


最新评论