Android 数据库文件存取至储存卡的方法
废话不多说了,直接给大家贴代码了,具体代码如下
<?xmlversion="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> packagecom.example.yanlei.wifi; importandroid.os.Bundle; importandroid.os.Environment; importandroid.support.v7.app.AppCompatActivity; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.Toast; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.PrintStream; importjava.util.Scanner; publicclassMainActivityextendsAppCompatActivity{ privateButtonbtnSave=null; privateButtonbtnRead=null; privateFilefile=null; privatestaticfinalStringFILENAME="data.txt"; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSave=(Button)super.findViewById(R.id.save); btnRead=(Button)super.findViewById(R.id.read); btnSave.setOnClickListener(newOnClickListener(){ publicvoidonClick(Viewv) { PrintStreamps=null; //判断外部存储卡是否存在 if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ Toast.makeText(getApplicationContext(),"读取失败,SD存储卡不存在!",Toast.LENGTH_LONG).show(); return; } //初始化File Stringpath=Environment.getExternalStorageDirectory().toString() +File.separator +"genwoxue" +File.separator +FILENAME; file=newFile(path); //如果当前文件的父文件夹不存在,则创建genwoxue文件夹 if(!file.getParentFile().exists()) file.getParentFile().mkdirs(); //写文件 try{ ps=newPrintStream(newFileOutputStream(file)); ps.println("跟我学网址:www.genwoxue.com"); ps.println(""); ps.println("电子邮件:hello@genwoxue.com"); }catch(FileNotFoundExceptione){ e.printStackTrace(); }finally{ ps.close(); } Toast.makeText(getApplicationContext(),"保存成功!",Toast.LENGTH_LONG).show(); } }); btnRead.setOnClickListener(newOnClickListener(){ publicvoidonClick(Viewv) { StringBufferinfo=newStringBuffer(); //判断外部存储卡是否存在 if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ Toast.makeText(getApplicationContext(),"读取失败,SD存储卡不存在!",Toast.LENGTH_LONG).show(); return; } //初始化File Stringpath=Environment.getExternalStorageDirectory().toString() +File.separator +"genwoxue" +File.separator +FILENAME; file=newFile(path); if(!file.exists()){ Toast.makeText(getApplicationContext(),"文件不存在!",Toast.LENGTH_LONG).show(); return; } //读取文件内容 Scannerscan=null; try{ scan=newScanner(newFileInputStream(file)); while(scan.hasNext()){ info.append(scan.next()).append("☆☆☆\n"); } Toast.makeText(getApplicationContext(),info.toString(),Toast.LENGTH_LONG).show(); }catch(FileNotFoundExceptione){ e.printStackTrace(); }finally{ scan.close(); } } }); } }
权限
<?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yanlei.wifi"> <!--在SDCard中创建与删除文件权限--> <uses-permissionandroid:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!--往SDCard写入数据权限--> <uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permissionandroid: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> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest> <uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
以上所述是小编给大家介绍的Android数据库文件存取至储存卡的方法,希望对大家有所帮助,本文写的不好还请各位大侠见谅!