Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android 数据库文件存取至储存卡的方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://scheR_856_11845@as.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(),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://scheR_856_11845@as.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 数据库文件存取至储存卡的方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。