灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:3120回复:0

android学习笔记8----------数据的存储与访问(2)

楼主#
更多 发布于:2012-09-06 13:44


承接
Android学习笔记6----------数据的存储与访问(1)



最后:
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>


喜欢0 评分0
游客

返回顶部