android 資料檔案存取至儲存卡,android存取

來源:互聯網
上載者:User

android 資料檔案存取至儲存卡,android存取

來自:http://blog.csdn.net/jianghuiquan/article/details/8569233

<?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" />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.