android的文件IO

//文件可以存在/sdcard目錄下,或者/data/data/our_package/下

//獲得sdcard的目錄方法  String path=Environment.getExternalStorageDirectory().getAbsolutePath();
package com.lhp;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class FileIOActivity extends Activity {
	
	
	EditText inputArea;
	Button writeButton,readButton;
	TextView showArea;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        inputArea=(EditText)findViewById(R.id.inputArea);
        writeButton=(Button)findViewById(R.id.writeButton);
        readButton=(Button)findViewById(R.id.readButton);
        showArea=(TextView)findViewById(R.id.showArea);
        
        
        writeButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				try{
					//FileOutputStream和FileInputStream方法,參數有MODE_APPEND,MODE_PRIVATE,
					// MODE_WORLD_WRITEABLE,MODE_WORLD_READABLE,	文件默認存放在data目錄下的
					//  /data/<package name>/files/下
					/*
					FileOutputStream fos=FileIOActivity.this.openFileOutput("lhp.txt",MODE_APPEND);
					fos.write(inputArea.getText().toString().getBytes());//字節寫入
					fos.close();
					*/
					
					/*
					 file=new File(PATH,NAME);
					 file.createFile(); //已存在就返回0,否則創建並返回1
					 FileOutputStream fos=new FileOutputStream(file);
					 fos.write(infoToWrite.getBytes());
					 fos.close();
					*/
					
					//第三種方法
					File dir = Environment.getDataDirectory();//得到data目錄  
	                File outFile=new File(dir,"/data/com.lhp/lhp.txt"); //只能在自己的程序包裏建立文件,這是權限問題
	                if(!outFile.createNewFile()){
	                	
						Toast.makeText(FileIOActivity.this,"創建文件成功",Toast.LENGTH_SHORT).show();
	                }
					FileWriter fw=new FileWriter(outFile,true);
					BufferedWriter bw=new BufferedWriter(fw);//或者不用buffer包裝,直接用FileWriter寫
					bw.write(inputArea.getText().toString());
					bw.flush();
					bw.close();	
					Toast.makeText(FileIOActivity.this,"寫入文件成功",Toast.LENGTH_SHORT).show();
				}catch(FileNotFoundException e2){
					Toast.makeText(FileIOActivity.this,"FileNotFoundException!",Toast.LENGTH_SHORT).show();
					e2.printStackTrace();
				}catch(IOException e){
					Toast.makeText(FileIOActivity.this,"IOException",Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
			}
		});
        
        readButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try{
					File dir = Environment.getDataDirectory();//得到/data目錄  
	                File inFile=new File(dir,"/data/com.lhp/lhp.txt"); 
				
					FileInputStream fileIn=new FileInputStream(inFile);
					
					int length=(int)inFile.length();
					byte[] message=new byte[length];
					fileIn.read(message);
					fileIn.close();
					String msg="";
					for(int i=0;i<length;i++)
					{
						msg+=(char)message[i];
					}
					showArea.setText(msg);
					Toast.makeText(FileIOActivity.this,"讀出文件成功",Toast.LENGTH_SHORT).show();
				}catch(FileNotFoundException e2){
					e2.printStackTrace();
				}catch(IOException e){
					e.printStackTrace();
				}
			}
		});
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章