你的位置:首页 > 操作系统

[操作系统]android之旅——开始


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 block				e.printStackTrace();			}		} catch (FileNotFoundException e) {			// TODO Auto-generated catch block			e.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.
 <!-- 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 block			e.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 block			e.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