Android--文件读写
2015-12-08 22:36
495 查看
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);
}
}
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);
}
}
相关文章推荐
- 在Android中操作JSON数据-读取JSON格式数据/创建JSON格式数据
- 在Android中操作XML数据-读取与解析XML数据/生成与输出XML数据
- 【Android】Facebook 登陆APP ID获取 android hash key获取,development/ release
- 《Android源码设计模式解析与实战》读书笔记(二)
- 使用 IntelliJ IDEA 开发 Android 应用程序时配置 Allatori 进行代码混淆
- Android Studio——自定义ListView子项目布局
- Android ORM框架GreenDao用法
- Android开发 详解嵌套ListView、ScrollView布局显示不全的问题
- Android 反射实战
- Android-View-绘制
- 读《50 Android Hacks》笔记整理Hack 31~Hack 34
- android中正确保存view的状态
- android Fragment 实现切换卡效果
- 【Android游戏开发二十七】讲解游戏开发与项目下的hdpi 、mdpi与ldpi资源文件夹以及游戏高清版本的设置
- 【Android游戏开发二十六】追加简述SurfaceView 与 GLSurfaceView效率!
- 【Android开发学习笔记】【高级】【随笔】插件化——资源加载
- 【Android游戏开发二十四】360°平滑游戏摇杆(触屏方向导航)
- 【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!简述J2me的游戏类库与Android游戏开发!
- 【Android】getwidth和getmeasuredwidth的区别以及两者的使用场景
- 【Android游戏开发二十一】Android os设备谎言分辨率的解决方案!以及简单阐述游戏引擎如何使用!