zoukankan      html  css  js  c++  java
  • Android--文件读写

    package com.zx.filedemo2;

    import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;


    public class MainActivity extends Activity {
    private TextView textView;
    private Button btnButton;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView=(TextView) findViewById(R.id.textView1);
    btnButton=(Button) findViewById(R.id.button1);
    editText=(EditText) findViewById(R.id.editText1);

    btnButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO 自动生成的方法存根
    writeDate(editText.getText().toString());
    textView.setText(readDate());
    }
    });
    }

    public void writeDate(String contentString) {
    try {
    FileOutputStream stream = openFileOutput("a.txt", MODE_PRIVATE);
    stream.write(contentString.getBytes());
    stream.close();
    } catch (FileNotFoundException e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    } catch (IOException e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }
    }

    public String readDate() {
    String str=null;
    try {
    FileInputStream stream = openFileInput("a.txt");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = stream.read(buffer))!=-1) {
    baos.write(buffer,0,len);
    }
    str = baos.toString();
    baos.close();
    stream.close();
    } catch (FileNotFoundException e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    } catch (IOException e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }

    return str;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
    return true;
    }
    return super.onOptionsItemSelected(item);
    }
    }

  • 相关阅读:
    bootstrap学习笔记一: bootstrap初认识,hello bootstrap(下)
    bootstrap学习笔记一: bootstrap初认识,hello bootstrap(上)
    AutoCompleteTextView的使用
    常用的android弹出对话框
    PopupWindow的使用
    linux udev、mdev 介绍
    linux 守护进程编程
    linux 下的文件目录操作之遍历目录
    linux 下查找图片文件方法
    linux 内核 zImage 生成过程分析
  • 原文地址:https://www.cnblogs.com/zxcnn/p/5031303.html
Copyright © 2011-2022 走看看