TQC-207 讀寫檔案

TQC-207 讀寫檔案

這是TQC的207題,

程式碼為自行撰寫,

僅供參考。

 

主要練習如何讀寫檔案。

 

package COM.TQC.GDD02;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

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

public class GDD02 extends Activity {
	private String btnAdd = "Add";
	private String btnLogin = "Login";
	private String btnUpdate = "Update";
	private String FileName = "MY_PASSWORD";
	private FileInputStream fileInputStream = null; 
	private Button Button01;
	private EditText EditText01;
	private TextView textView1;

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        findview();
        setlistener();
		
    }
    
    public void findview()
    {
    	EditText01 = (EditText) findViewById(R.id.EditText01);
        Button01 = (Button) findViewById(R.id.Button01);
        textView1 = (TextView) findViewById(R.id.textView1);
        textView1.setText(readFile());
    }
    
    public void setlistener()
    {
    	if(fileInputStream==null){
			Button01.setText(btnAdd);
		}
		else{
			Button01.setText(btnLogin);
		}
		Button01.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Button01.getText().equals(btnAdd)){
					String text = ""+EditText01.getText();
					if(!text.equals("")){
						writeFile(text);
						showToast("新增成功!請重新登入!"); 
						Button01.setText(btnLogin);
						EditText01.setText("");
					}
					else{
						showToast("請輸入密碼!"); 
					}
					textView1.setText(readFile());
				}
				else if(Button01.getText().equals(btnLogin)){
					String text = ""+EditText01.getText();
					if(text.equals(readFile())){
						showToast("登入成功!");
						Button01.setText(btnUpdate);
					}
					else{
						showToast("登入失敗!");
					}
					textView1.setText(readFile());
				}
				else if(Button01.getText().equals(btnUpdate)){
					String text = ""+EditText01.getText();
					if(!text.equals("")){
						writeFile(text);
						showToast("修改成功!請重新登入!");
						Button01.setText(btnLogin);
						EditText01.setText("");
					}
					else{
						showToast("請輸入密碼!"); 
					}
					textView1.setText(readFile());
				}
			}});
    }
    
    private void writeFile(String data)
    {
    	FileOutputStream fOut = null;
    	OutputStreamWriter osw = null; 
    	try
    	{
    		fOut = openFileOutput(FileName,MODE_PRIVATE);
    		osw = new OutputStreamWriter(fOut);
    		osw.write(data);
    		osw.flush();
    		showToast("AOK");
    	}
    	catch (Exception e) 
    	{
    		e.printStackTrace();
    	}
    	finally 
    	{
    		try 
    		{
    			osw.close();
    			fOut.close();
    		} 
    		catch (IOException e) 
    		{
    			e.printStackTrace();
    		}
    	} 
			
			
		 
    }
    private String readFile(){
    	
		String data = ""; 
		FileInputStream fIn = null;
		InputStreamReader isr = null;
		char[] inputBuffer = new char[255];

		try
		{
			fIn = openFileInput(FileName);
			isr = new InputStreamReader(fIn);		
			isr.read(inputBuffer);
			data = new String(inputBuffer);
			data=data.trim();		
			showToast("ROK");
		}
		catch (Exception e) 
		{
			e.printStackTrace();		
		}
		finally 
		{
			try 
			{
				isr.close();
				fIn.close();
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
		}
		return data;
    }
    private void showToast(String message){
    	Toast.makeText(this, message, 0).show();
    }
}

 

 

自我LV~