android之旅——開始

來源:互聯網
上載者:User

標籤:

1、檔案的讀取

io流讀取檔案,並且顯示

package com.helloword;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;public class ReadFile {private String text = null;private StringBuffer strbuf=null;public void ReadFile(File file)  {// TODO Auto-generated constructor stub//擷取檔案輸出資料流FileInputStream fis;try {fis = new FileInputStream(file);//將位元組流轉換為字元流BufferedReader br = new BufferedReader(new InputStreamReader(fis));try {text = br.readLine();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}strbuf.append(text);System.out.println(text);}}

  其中訪問android的"data/data/com.helloword/file"建立過程如下    

  開啟file explore,即可看到Android 的檔案

  開啟cmd,進入sdk platform-tool

  >adb shell
  $ su
  # chmod 777 /data

  # chmod 777 /data/data

public class MainActivity extends Activity {public Button bt =null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bt = (Button) findViewById(R.id.btcon);bt.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubFile file = new File("data/data/com.helloword/file");ReadFile readfile = new ReadFile();}});}

  


2、SD卡檔案的讀寫操作

  (1)在manifest.xml中註冊,獲得SD卡的讀寫權限
 <!-- SDCard中建立與刪除檔案許可權 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><!-- 向SDCard寫入資料許可權 -->   <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

  (2) 接著在使用SDcard進行讀寫的時候 會用到Environment類下面的幾個靜態方法  : 

      1: getDataDirectory() 擷取到Android中的data資料目錄(sd卡中的data檔案夾)
      2:getDownloadCacheDirectory() 擷取到下載的緩衝目錄(sd卡中的download檔案夾)
      3:getExternalStorageDirectory() 擷取到外部儲存的目錄 一般指SDcard(/storage/sdcard0)
      4:getExternalStorageState() 擷取外部設定的目前狀態 一般指SDcard,比較常用的應該是 MEDIA_MOUNTED(SDcard存在並且可以進行讀寫)還有其他的一些狀態,可以在文檔中進行尋找.

/** * 判斷SDCard是否存在 [當沒有外掛SD卡時,內建ROM也被識別為存在sd卡] *  * @return */public static boolean isSdCardExist() {return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);}

  

      * 使用api獲得sd卡的真實路徑,部分手機品牌會更改sd卡的路徑

        Environment.getExternalStorageDirectory();

讀取SD卡的內容

  

//讀取SD卡內容//使用FileInputStream讀取檔案public String ReadFlieInputString(String FileName) throws IOException{String result = null;File file = new File(FileName);try {FileInputStream isfile = new FileInputStream(file);byte[] b = new byte[isfile.available()];isfile.read(b);result = new String(b);System.out.print("讀取成功:"+ result);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return result;}//使用BufferRead 讀取檔案public String FileBufferRead(String FileName) throws IOException {String result = null;try {BufferedReader bReader = new BufferedReader(new FileReader(FileName));String oneline = "";StringBuffer sb = new  StringBuffer();while ((oneline = bReader.readLine()) != null) {sb.append(oneline);}result = sb.toString();bReader.close();System.out.println("讀取成功");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return result;}

  向SD卡中寫入檔案

//寫入SD卡//使用FileOutputStream寫入檔案public Boolean writeSDFile(String FileName, String content){boolean result = false;try {File file = new File(Environment.getExternalStorageDirectory(), FileName);//獲得輸出資料流        FileOutputStream fos = new FileOutputStream(file);            fos.write(content.getBytes());            fos.close();System.out.println("寫入成功:");result = true;} catch (Exception e) {e.printStackTrace();}return result;}//使用buffread寫入SD卡public Boolean BufferWriteFile(String FileName, String content){boolean result = false;try {File file = new File(Environment.getExternalStorageDirectory(),FileName);//第二個參數意義是說是否以append方式新增內容BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));bw.write(content);bw.flush();System.out.println("寫入成功");                        result = true;} catch (Exception e) {e.printStackTrace();}return result;}                 

  以上內容純屬自己練著玩!基本都是參照http://blog.csdn.net/mad1989/article/details/37568667

 

android之旅——開始

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.