android學習筆記8———-數據的存儲與訪問(2)

承接

android學習筆記6———-數據的存儲與訪問(1)

/kf/201202/119866.html

最後:

Activity的設計。

效果圖:

 

 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
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; 
 
import com.luku.file.service.FileService; 
 
public class fileActivity extends Activity 

    private EditText editText=null;  
    private TextView content =null;  
    private TextView textView1=null; 
    private Button   read=null; 
    private Button   button =null; 
    private Button   exit=null; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        textView1=(TextView)findViewById(R.id.textView1); 
        content=(TextView)findViewById(R.id.content); 
        editText=(EditText)findViewById(R.id.filename); 
        button=(Button)findViewById(R.id.baocun); 
        read=(Button)findViewById(R.id.read); 
        exit=(Button)findViewById(R.id.exit); 
         
        button.setOnClickListener(listener); 
        read.setOnClickListener(listener);       
        exit.setOnClickListener(listener); 
    } 
     
    private OnClickListener listener=new OnClickListener() 
    { 
        int resId = R.string.Success; 
        @Override 
        public void onClick(View v) 
        { 
            Button btn=(Button)v; 
            switch (btn.getId()) 
            { 
                case R.id.baocun: 
                    try 
                    { 
                        OutputStream stream=openFileOutput(editText.getText().toString(), Context.MODE_PRIVATE); 
                        FileService.save(stream, content.getText().toString()); 
                    }  
                    catch (IOException e) 
                    { 
                        e.printStackTrace(); 
                        resId = R.string.error; 
                        Toast.makeText(fileActivity.this, resId, Toast.LENGTH_LONG).show(); 
                    } 
                    Toast.makeText(fileActivity.this, resId, Toast.LENGTH_LONG).show(); 
                    break; 
                     
                case R.id.read: 
                    try 
                    { 
                        InputStream instream =openFileInput(editText.getText().toString()); 
                        String string=FileService.read(instream); 
                        System.out.println(string); 
                        textView1.setText(string.toString()); 
                    }  
                    catch (Exception e) 
                    { 
                        Toast.makeText(fileActivity.this, "讀取錯誤", Toast.LENGTH_LONG).show(); 
                    }        
                    break; 
     
                case R.id.exit: 
                    android.os.Process.killProcess(android.os.Process.myPid());                  
                    break; 
                     
                default: 
                    break; 
            } 
             
        } 
    }; 

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

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
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;

import com.luku.file.service.FileService;

public class fileActivity extends Activity
{
 private EditText editText=null; 
 private TextView content =null; 
 private TextView textView1=null;
 private Button   read=null;
 private Button   button =null;
 private Button   exit=null;
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  textView1=(TextView)findViewById(R.id.textView1);
  content=(TextView)findViewById(R.id.content);
  editText=(EditText)findViewById(R.id.filename);
  button=(Button)findViewById(R.id.baocun);
  read=(Button)findViewById(R.id.read);
  exit=(Button)findViewById(R.id.exit);
  
  button.setOnClickListener(listener);
  read.setOnClickListener(listener);  
  exit.setOnClickListener(listener);
 }
 
 private OnClickListener listener=new OnClickListener()
 {
  int resId = R.string.Success;
  @Override
  public void onClick(View v)
  {
   Button btn=(Button)v;
   switch (btn.getId())
   {
    case R.id.baocun:
     try
     {
      OutputStream stream=openFileOutput(editText.getText().toString(), Context.MODE_PRIVATE);
      FileService.save(stream, content.getText().toString());
     }
     catch (IOException e)
     {
      e.printStackTrace();
      resId = R.string.error;
      Toast.makeText(fileActivity.this, resId, Toast.LENGTH_LONG).show();
     }
     Toast.makeText(fileActivity.this, resId, Toast.LENGTH_LONG).show();
     break;
     
    case R.id.read:
     try
     {
      InputStream instream =openFileInput(editText.getText().toString());
      String string=FileService.read(instream);
      System.out.println(string);
      textView1.setText(string.toString());
     }
     catch (Exception e)
     {
      Toast.makeText(fileActivity.this, "讀取錯誤", Toast.LENGTH_LONG).show();
     }  
     break;
 
    case R.id.exit:
     android.os.Process.killProcess(android.os.Process.myPid());     
     break;
     
    default:
     break;
   }
   
  }
 };
}

 

main.xml文件

[java]
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
     
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 
             
            <TextView   
                android:layout_width="90px"  
                android:layout_height="wrap_content"  
                android:text="文件名稱:" 
                android:textSize="21px" 
                android:id="@+id/filenamelebal" 
                /> 
             
            <EditText 
                 android:layout_width="fill_parent"  
                 android:layout_height="wrap_content"  
                 android:layout_toRightOf="@id/filenamelebal" 
                 android:layout_alignTop="@id/filenamelebal" 
                 android:id="@+id/filename" 
            ></EditText> 
        ></RelativeLayout> 
         
        <TextView   
                android:layout_width="90px"  
                android:layout_height="wrap_content"  
                android:text="內容:" 
                android:textSize="21px" 
                /> 
        <EditText 
                 android:layout_width="fill_parent"  
                 android:layout_height="wrap_content"  
                 android:minLines="6" 
                 android:id="@+id/content" 
            ></EditText> 
        <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content"> 
            <Button  
                android:layout_height="wrap_content"  
                android:id="@+id/baocun"  
                android:text="保存"  
                android:layout_width="wrap_content"></Button> 
            <Button  
                android:text="讀取"  
                android:id="@+id/read"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"></Button> 
            <Button  
                android:layout_width="wrap_content"  
                android:text="退出"  
                android:id="@+id/exit"  
                android:layout_height="wrap_content"></Button> 
                 
        </LinearLayout> 
        <TextView  
                android:text="內容讀取顯示"  
                android:id="@+id/textView1"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"> 
                </TextView> 
        
         
        
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">
      
      <TextView 
       android:layout_width="90px"
       android:layout_height="wrap_content"
       android:text="文件名稱:"
       android:textSize="21px"
       android:id="@+id/filenamelebal"
       />
      
      <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/filenamelebal"
        android:layout_alignTop="@id/filenamelebal"
        android:id="@+id/filename"
      ></EditText>
     ></RelativeLayout>
     
     <TextView 
       android:layout_width="90px"
       android:layout_height="wrap_content"
       android:text="內容:"
       android:textSize="21px"
       />
  <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minLines="6"
        android:id="@+id/content"
      ></EditText>
     <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
         <Button
          android:layout_height="wrap_content"
          android:id="@+id/baocun"
          android:text="保存"
          android:layout_width="wrap_content"></Button>
         <Button
          android:text="讀取"
          android:id="@+id/read"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"></Button>
         <Button
          android:layout_width="wrap_content"
          android:text="退出"
          android:id="@+id/exit"
          android:layout_height="wrap_content"></Button>
          
     </LinearLayout>
     <TextView
       android:text="內容讀取顯示"
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
       </TextView>
   
    
   
</LinearLayout>

 

FileService文件

[java]
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
 
public class FileService 

    /**
     * 保存數據
     * 
     * @param stream
     * @param content
     * @throws IOException
     */ 
    public static void save(OutputStream stream, String content) 
            throws IOException 
    { 
        stream.write(content.getBytes()); 
        stream.close(); 
    } 
 
    /**
     * 讀取數據
     * 
     * @param inStream
     * @return
     * @throws IOException
     */ 
    public static String read(InputStream inStream) throws IOException 
    { 
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
        int len = -1; 
        byte[] buffer = new byte[1024]; 
        while ((len = inStream.read(buffer)) != -1) 
        { 
            outputStream.write(buffer, 0, len); 
        } 
        byte[] data = outputStream.toByteArray(); 
        inStream.close(); 
        outputStream.close(); 
        return new String(data); 
 
    } 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileService
{
 /**
  * 保存數據
  *
  * @param stream
  * @param content
  * @throws IOException
  */
 public static void save(OutputStream stream, String content)
   throws IOException
 {
  stream.write(content.getBytes());
  stream.close();
 }

 /**
  * 讀取數據
  *
  * @param inStream
  * @return
  * @throws IOException
  */
 public static String read(InputStream inStream) throws IOException
 {
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  int len = -1;
  byte[] buffer = new byte[1024];
  while ((len = inStream.read(buffer)) != -1)
  {
   outputStream.write(buffer, 0, len);
  }
  byte[] data = outputStream.toByteArray();
  inStream.close();
  outputStream.close();
  return new String(data);

 }

 

點擊保存

 

點擊讀取

 

補充:使用文件的絕對路徑訪問文件

(1)

File file=new File("/data/data/包名/files/2.txt");

FileInputStream inStream=new  FileInputStream(file);

Log.i("FileTest",readInStream(inStream));

(2)

把文件存入SD卡中

首先加入讀寫權限

SD卡創建與刪除文件權限

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

SD卡寫入數據權限

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

 

 摘自 奔跑的蝸牛

 

 

發佈留言