Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中SD卡讀寫操作

Android中SD卡讀寫操作

編輯:關於Android編程

注意:不直接進行讀出是為了防止打文件操作對內存的消耗,所以用輸入流讀到硬盤上。

public String readFile(String fileName) throws Exception{
		FileInputStream fis = context.openFileInput(fileName);
		byte[] bytes = new byte[fis.available()];
		fis.read(bytes);
		fis.close();
		return new String(bytes,"utf-8");
	}
當文件很大的時候,byte[]會占用很大的內存。

package cn.itcast.fileio.service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.os.Environment;

public class SdCardService {
	private Context ctx;

	public SdCardService(Context ctx) {
		this.ctx = ctx;
	}

	/**
	 * 寫文件入skcard
	 */
	public void writeToSdCard(String fileName, String cont) {
		try {
			// 判斷是否有掛載sdcard
			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {
				// 得到sdcar文件目錄
				File dir = Environment.getExternalStorageDirectory();
				File file = new File(dir, "itcast.txt");
				FileOutputStream fos = new FileOutputStream(file);
				fos.write(cont.getBytes());
				fos.close();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 讀SdCard中的文件
	 */
	public String readSdCard(String fileName) {
		try {
			// 判斷是否有掛載sdcard
			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {
				// 得到sdcar文件目錄
				File dir = Environment.getExternalStorageDirectory();
				File file = new File(dir, "itcast.txt");
				FileInputStream fis = new FileInputStream(file);
				return readIs2String(fis);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 將輸入流數據讀取到輸出流當中
	 */
	private OutputStream readIs2Os(InputStream is ,OutputStream os){
		try {
			byte[] bytes = new byte[1024];
			int length = 0 ;
			while((length = is.read(bytes)) != -1){
				os.write(bytes, 0, length);
			}
			is.close();
			os.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return os ;
	}
	
	/**
	 * 將輸入流數據讀取到輸出流當中
	 */
	public byte[] readIs2Bytes(InputStream is){
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		readIs2Os(is,baos);
		return baos.toByteArray() ;
	}
	
	public String readIs2String(InputStream is){
		try {
			return new String(readIs2Bytes(is),"utf-8");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null ;
	}
	
	public String readIs2String(String fileName){
		try {
			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
				File dir = Environment.getExternalStorageDirectory();
				File file = new File(dir,fileName);
				FileInputStream is = new FileInputStream(file);
				return readIs2String(is);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null ;
	}
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved