Android 數(shù)據(jù)庫文件存取至儲(chǔ)存卡的方法
廢話不多說了,直接給大家貼代碼了,具體代碼如下
<?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="保存數(shù)據(jù)(File)" />
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="讀取數(shù)據(jù)(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;
//判斷外部存儲(chǔ)卡是否存在
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(getApplicationContext(), "讀取失敗,SD存儲(chǔ)卡不存在!", Toast.LENGTH_LONG).show();
return;
}
//初始化File
String path=Environment.getExternalStorageDirectory().toString()
+File.separator
+"genwoxue"
+File.separator
+FILENAME;
file=new File(path);
//如果當(dāng)前文件的父文件夾不存在,則創(chuàng)建genwoxue文件夾
if(!file.getParentFile().exists())
file.getParentFile().mkdirs();
//寫文件
try {
ps = new PrintStream(new FileOutputStream(file));
ps.println("跟我學(xué)網(wǎng)址: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();
//判斷外部存儲(chǔ)卡是否存在
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(getApplicationContext(), "讀取失敗,SD存儲(chǔ)卡不存在!", 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;
}
//讀取文件內(nèi)容
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();
}
}
});
}
}
權(quán)限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yanlei.wifi" > <!-- 在SDCard中創(chuàng)建與刪除文件權(quán)限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard寫入數(shù)據(jù)權(quán)限 --> <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 數(shù)據(jù)庫文件存取至儲(chǔ)存卡的方法,希望對大家有所幫助,本文寫的不好還請各位大俠見諒!
- Android編程中FileOutputStream與openFileOutput()的區(qū)別分析
- Android 數(shù)據(jù)存儲(chǔ)之 FileInputStream 工具類及FileInputStream類的使用
- android實(shí)現(xiàn)Uri獲取真實(shí)路徑轉(zhuǎn)換成File的方法
- Android學(xué)習(xí)筆記-保存文件(Saving Files)
- Android類FileDownloadList分析
- android開發(fā)教程之獲取power_profile.xml文件的方法(android運(yùn)行時(shí)能耗值)
- WAC啟動(dòng)Android模擬器 transfer error: Read-only file system錯(cuò)誤解決方法
- 實(shí)例詳解Android文件存儲(chǔ)數(shù)據(jù)方式
- Android使用文件進(jìn)行數(shù)據(jù)存儲(chǔ)的方法
- Android采用File形式保存與讀取數(shù)據(jù)的方法
相關(guān)文章
自定義TextView跑馬燈效果可控制啟動(dòng)/停止/速度/焦點(diǎn)
Android自帶的跑馬燈效果不太好控制,不能控制速度,不能即時(shí)停止和啟動(dòng),而且還受焦點(diǎn)的影響不已,由于項(xiàng)目需求需所以自己寫了一個(gè)自定義的TextView,感興趣的朋友可以了解下2013-01-01
詳解如何使用VisualStudio高效開發(fā)調(diào)試AndroidNDK
這篇文章主要介紹了詳解如何使用VisualStudio高效開發(fā)調(diào)試AndroidNDK,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
Android開發(fā):微信授權(quán)登錄與微信分享完全解析
本篇文章主要介紹了Android微信授權(quán)登錄與微信分享,具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11
Android studio 3.0 查看手機(jī)文件系統(tǒng)的方法(超簡單)
本文給大家分享Android studio更新到3.0版本之后,查看手機(jī)文件系統(tǒng)的方法,需要的朋友參考下吧2017-11-11
Android使用Jetpack WindowManager開發(fā)可折疊設(shè)備(過程分享)
這篇文章主要介紹了Android使用Jetpack WindowManager開發(fā)可折疊設(shè)備,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11
Android通過ImageView設(shè)置手指滑動(dòng)控件縮放
這篇文章主要介紹了Android通過ImageView設(shè)置手指滑動(dòng)控件縮放效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12
Android框架學(xué)習(xí)之Volley和Glide詳解
這篇文章主要給大家介紹了關(guān)于Android框架學(xué)習(xí)之Volley和Glide的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
Android編程實(shí)現(xiàn)自定義Dialog的大小自動(dòng)控制方法示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義Dialog的大小自動(dòng)控制方法,結(jié)合實(shí)例形式分析了Android自定義Dialog對話框的屬性操作技巧與大小動(dòng)態(tài)控制實(shí)現(xiàn)方法,需要的朋友可以參考下2017-09-09
不依賴于Activity的Android全局懸浮窗的實(shí)現(xiàn)
在Android應(yīng)用開發(fā)中,經(jīng)常要遇到做全局懸浮窗的效果,本文的內(nèi)容主要是如何不依賴于Activity的全局懸浮窗的實(shí)現(xiàn)及原理,有需要的可以參考。2016-07-07
AndroidStudio接入U(xiǎn)nity工程并實(shí)現(xiàn)相互跳轉(zhuǎn)的示例代碼
這篇文章主要介紹了AndroidStudio接入U(xiǎn)nity工程并實(shí)現(xiàn)相互跳轉(zhuǎn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12

