Android文件读写

package com.xiva;

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.test.AndroidTestCase;
import android.util.Log;

public class UnitTest extends AndroidTestCase{
	
	private static final String Tag = "UnitTest";
	
	public void testCall() throws Exception{
		int i = 1+1;
		i = this.compute(i);
		Log.i(Tag,"result="+i);
	}
	
	/**
	 * @description 访问其他文件
	 * @throws Exception
	 */
	public void testRead() throws Exception{
		File file = new File("/data/data/com.xiva.file/files/test.txt");
		InputStream iStream = new FileInputStream(file);
		String result = read(iStream);
		Log.i(Tag, result);
	}
	
	/**
	 * 
	 * @throws Exception
	 */
	public void testWrite() throws Exception{
		File file = new File("/data/data/com.xiva.file/files/test.txt");
		FileOutputStream iStream = new FileOutputStream(file);
		save(iStream,"test");
	}
	
	public int compute(int FirNum){
		int secondNum = 10;
		int result = secondNum + FirNum;
		
		return result;
	}
	
	public static String read(InputStream iStream) throws IOException{
		ByteArrayOutputStream oStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = -1;
		while((len = iStream.read(buffer)) != -1){
			oStream.write(buffer, 0, len);
		}
		byte[] data = oStream.toByteArray();
		iStream.close();
		oStream.close();
		return new String(data);
	}
	
	public static void save(OutputStream oStream,String content) throws IOException{
		oStream.write(content.getBytes());
		oStream.close();
	}
	
}

贴出主要代码,其他代码见附件!

package com.xiva.file;

import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class FileActivity extends Activity {
	
	private EditText fileName;
	private EditText content;
	private TextView textContent;
	private static final String TAG = "FileActivity";
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        fileName = (EditText) this.findViewById(R.id.fileName);
        content  = (EditText) this.findViewById(R.id.content);
        textContent = (TextView) this.findViewById(R.id.textContent);
        Button saveBtn = (Button)this.findViewById(R.id.saveButton);
        Button viewBtn = (Button)this.findViewById(R.id.viewButton);
        
        saveBtn.setOnClickListener(listener);//保存按钮事件监听
        viewBtn.setOnClickListener(listener);//查看按钮事件监听
    }
    
    private View.OnClickListener listener = new View.OnClickListener(){
    	    	
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Button button     = (Button) v;
			String nameStr    = fileName.getText().toString().trim();
	        String contentStr = content.getText().toString();
	        
	        switch(button.getId())
	        {
	        	case R.id.saveButton :
	    	        int resId_s         = R.string.success;
	    	        OutputStream fileOS = null;
	    			try {
//	    				Context.MODE_APPEND----已append的模式保存文件内容,
//	    				将现在的内容追加到原有文件内容之后
//	    				Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,
//	    				只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
//	    				Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE
//	    				用来控制其他应用是否有权限读写该文件
	    				fileOS  = FileActivity.this.openFileOutput(nameStr+".txt",
	    						Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE + Context.MODE_APPEND);
	    				//下面写法即有读权限,也有写权限;如若又想用追加方式 ,加上"+Context.MODE_APPEND"即可.
//	    				fileOS  = FileActivity.this.openFileOutput(nameStr+".txt",
//	    						Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
	    				FileService.save(fileOS, contentStr);
	    			} catch (Exception e) {
	    				resId_s = R.string.failure;
	    				e.printStackTrace();
	    			}
	    			//界面提示
	    			Toast.makeText(FileActivity.this, resId_s, Toast.LENGTH_LONG).show();
	    			Log.i(TAG, nameStr);
	    			Log.i(TAG, contentStr);
	    			break;
	        	case R.id.viewButton :
	        		int resId_v 	   = R.string.readSucc;
	        		InputStream fileIS = null;
	        		String contentSt   = null;
					try {
						fileIS    = FileActivity.this.openFileInput(nameStr+".txt");
						contentSt = FileService.read(fileIS);
					} catch (Exception e) {
						resId_v = R.string.readFail;
						e.printStackTrace();
					}
					textContent.setText(contentSt);
					Log.i(TAG, contentSt);
					Toast.makeText(FileActivity.this, resId_v, Toast.LENGTH_LONG).show();
					Log.i(TAG, nameStr);
					break;
	        }

		}
    	   	
    };
    
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值