欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 数据库文件存取至储存卡的方法

程序员文章站 2024-02-27 12:19:09
废话不多说了,直接给大家贴代码了,具体代码如下 <...

废话不多说了,直接给大家贴代码了,具体代码如下

<?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 数据库文件存取至储存卡的方法,希望对大家有所帮助,本文写的不好还请各位大侠见谅!