Android 數(shù)據(jù)庫(kù)文件存取至儲(chǔ)存卡的方法
廢話不多說(shuō)了,直接給大家貼代碼了,具體代碼如下
<?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ù)庫(kù)文件存取至儲(chǔ)存卡的方法,希望對(duì)大家有所幫助,本文寫的不好還請(qǐng)各位大俠見(jiàn)諒!
- 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)文章
android實(shí)現(xiàn)圖片橡皮擦和快速染色功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)圖片橡皮擦和快速染色功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android Studio綁定下拉框數(shù)據(jù)詳解
這篇文章主要為大家詳細(xì)介紹了Android Studio綁定下拉框數(shù)據(jù),Android Studio綁定網(wǎng)絡(luò)JSON數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android自定義View中attrs.xml的實(shí)例詳解
這篇文章主要介紹了Android自定義View中attrs.xml的實(shí)例詳解的相關(guān)資料,在自定義View首先對(duì)attrs.xml進(jìn)行布局的實(shí)現(xiàn)及屬性的應(yīng)用,需要的朋友可以參考下2017-07-07
Ubuntu中為Android HAL編寫JNI方法提供JAVA訪問(wèn)硬件服務(wù)接口
本文主要介紹Ubuntu中為Android硬件抽象層模塊編寫JNI方法提供Java訪問(wèn)硬件服務(wù)接口,這里給大家詳細(xì)說(shuō)明如何編寫 JNI方法訪問(wèn)硬件接口并附示例代碼,有需要的小伙伴參考下2016-08-08
flutter 動(dòng)手?jǐn)]一個(gè)城市選擇citypicker功能
在一些項(xiàng)目開發(fā)中經(jīng)常會(huì)用到城市選擇器功能,今天小編動(dòng)手?jǐn)]一個(gè)基于flutter 城市選擇citypicker功能,具體實(shí)現(xiàn)過(guò)程跟隨小編一起看看吧2021-08-08
android實(shí)現(xiàn)緩存圖片等數(shù)據(jù)
本文給大家分享的是Android采用LinkedHashMap自帶的LRU 算法緩存數(shù)據(jù)的方法和示例,有需要的小伙伴可以參考下。2015-07-07
Android實(shí)現(xiàn)帶列表的地圖POI周邊搜索功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶列表的地圖POI周邊搜索功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
Android如何通過(guò)手機(jī)自動(dòng)獲取短信驗(yàn)證碼
注冊(cè)帳號(hào)時(shí),經(jīng)常需要手機(jī)獲取驗(yàn)證碼,Android如何通過(guò)手機(jī)自動(dòng)獲取短信驗(yàn)證碼,下面看看小編給大家分享的一段代碼,感興趣的小伙伴們可以參考一下2016-03-03
Android利用Intent實(shí)現(xiàn)讀取圖片操作
這篇文章主要為大家詳細(xì)介紹了Android利用Intent實(shí)現(xiàn)讀取圖片操作的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-06-06
Android 自定義View之倒計(jì)時(shí)實(shí)例代碼
這篇文章主要介紹了Android 自定義View之倒計(jì)時(shí)實(shí)例代碼的相關(guān)資料,大多數(shù)app在注冊(cè)的時(shí)候,都有一個(gè)獲取驗(yàn)證碼的按鈕,點(diǎn)擊后,訪問(wèn)接口,最終用戶會(huì)收到短信驗(yàn)證碼。為了不多次寫這個(gè)獲取驗(yàn)證碼的接口,下面將它自定義成一個(gè)view,方便使用,需要的朋友可以參考下2017-04-04

