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

作者:gisoracle 时间:2023-05-08 19:12:14 

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


<?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,数据,文件,存取卡
0
投稿

猜你喜欢

  • C#支付宝新版支付请求接口调用

    2023-02-23 15:50:45
  • Java实现AWT四大事件的详细过程

    2023-11-28 18:39:52
  • MaterialApp Flutter 应用全局配置与主题管理详解

    2023-05-03 18:50:48
  • DataGridView控件显示行号的正确代码及分析

    2022-07-26 22:05:50
  • JAVA集合框架Map特性及实例解析

    2021-06-05 02:23:39
  • Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)

    2023-10-20 10:00:50
  • android计算器代码示例分享

    2023-10-14 14:06:58
  • MyBatis注解式开发映射语句详解

    2023-06-07 20:31:23
  • Java 将List中的实体类按照某个字段进行分组并存放至Map中操作

    2022-09-13 20:25:35
  • Java8 如何移除两个相同的List对象

    2023-06-07 20:57:50
  • Java连接redis及基本操作示例

    2023-12-07 03:27:03
  • Java使用application.property读取文件里面的值

    2023-11-01 07:24:56
  • Android解析XML文件升级APK的方法

    2022-06-05 20:47:26
  • Java中用内存映射处理大文件的实现代码

    2023-11-05 06:16:41
  • java 垃圾回收机制以及经典垃圾回收器详解

    2022-07-06 05:16:08
  • Java同步容器和并发容器详解

    2023-03-22 06:45:28
  • C#编程实现查看剪切板内容的方法

    2022-01-17 07:06:03
  • java数据结构和算法学习之汉诺塔示例

    2021-09-28 14:46:18
  • c#的params参数使用示例

    2021-10-07 04:53:39
  • Flutter加载图片的多样玩法汇总

    2023-08-24 09:48:22
  • asp之家 软件编程 m.aspxhome.com