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实现图片橡皮擦和快速染色功能

    android实现图片橡皮擦和快速染色功能

    这篇文章主要为大家详细介绍了android实现图片橡皮擦和快速染色功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Android Studio绑定下拉框数据详解

    Android Studio绑定下拉框数据详解

    这篇文章主要为大家详细介绍了Android Studio绑定下拉框数据,Android Studio绑定网络JSON数据,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Android自定义View中attrs.xml的实例详解

    Android自定义View中attrs.xml的实例详解

    这篇文章主要介绍了Android自定义View中attrs.xml的实例详解的相关资料,在自定义View首先对attrs.xml进行布局的实现及属性的应用,需要的朋友可以参考下
    2017-07-07
  • Ubuntu中为Android HAL编写JNI方法提供JAVA访问硬件服务接口

    Ubuntu中为Android HAL编写JNI方法提供JAVA访问硬件服务接口

    本文主要介绍Ubuntu中为Android硬件抽象层模块编写JNI方法提供Java访问硬件服务接口,这里给大家详细说明如何编写 JNI方法访问硬件接口并附示例代码,有需要的小伙伴参考下
    2016-08-08
  • flutter 动手撸一个城市选择citypicker功能

    flutter 动手撸一个城市选择citypicker功能

    在一些项目开发中经常会用到城市选择器功能,今天小编动手撸一个基于flutter 城市选择citypicker功能,具体实现过程跟随小编一起看看吧
    2021-08-08
  • android实现缓存图片等数据

    android实现缓存图片等数据

    本文给大家分享的是Android采用LinkedHashMap自带的LRU 算法缓存数据的方法和示例,有需要的小伙伴可以参考下。
    2015-07-07
  • Android实现带列表的地图POI周边搜索功能

    Android实现带列表的地图POI周边搜索功能

    这篇文章主要为大家详细介绍了Android实现带列表的地图POI周边搜索功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • Android如何通过手机自动获取短信验证码

    Android如何通过手机自动获取短信验证码

    注册帐号时,经常需要手机获取验证码,Android如何通过手机自动获取短信验证码,下面看看小编给大家分享的一段代码,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • Android利用Intent实现读取图片操作

    Android利用Intent实现读取图片操作

    这篇文章主要为大家详细介绍了Android利用Intent实现读取图片操作的相关资料,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Android 自定义View之倒计时实例代码

    Android 自定义View之倒计时实例代码

    这篇文章主要介绍了Android 自定义View之倒计时实例代码的相关资料,大多数app在注册的时候,都有一个获取验证码的按钮,点击后,访问接口,最终用户会收到短信验证码。为了不多次写这个获取验证码的接口,下面将它自定义成一个view,方便使用,需要的朋友可以参考下
    2017-04-04

最新评论