android中file的使用实例
来源:互联网 发布:天猫美工工作描述 编辑:程序博客网 时间:2024/06/05 18:11
File是android的4种存储方式的一种。File就是文件的意思一个文件,你无非是想进行读写操作。所以这就用到两个流。一个数输入流,一个是输出流。FileOutstream,和FileInputSream。这两个和java基础的流是一样的。如果你对流不清楚可以我看看我以前写的java对流的使用。
下面我写一个android程序对file是的使用
布局
下面我就写一个actitvty的代码
package com.android.app;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class File extends Activity {
/** Called when the activity is firstcreated. */
private EditTextcontent;
private TextViewreadContent;
private ButtonwriteButton;
private ButtonreadButton;
private CheckBoxcheckBox;
private TextViewshowContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//绑定所有组件
content = (EditText) findViewById(R.id.EditText01);
readButton = (Button) findViewById(R.id.Button02);
writeButton = (Button) findViewById(R.id.Button01);
checkBox = (CheckBox) findViewById(R.id.CheckBox01);
showContent = (TextView) findViewById(R.id.TextView01);
readContent = (TextView) findViewById(R.id.TextView02);
//点击写按钮触发写入事件
writeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FileOutputStream fos = null;
try {
if (checkBox.isChecked()) {
//这个就是一个流文件的写入第一个参数是一个文件的文件的文件名,后面的文件的写入方式
fos = openFileOutput("fish", Context.MODE_APPEND);//追加的写入
} else {
fos = openFileOutput("fish", Context.MODE_PRIVATE);//覆盖的写入
}
String text = content.getText().toString();
fos.write(text.getBytes()); //将你输入的东西写入缓冲区。这里还没有在文件里面写
showContent.setText("写入成功");
content.setText("");
} catch (FileNotFoundException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} finally {
if (fos !=null) {
try {
fos.flush();//当执行完这个你的文件里面才有你写入的内容
fos.close();
} catch (IOException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
}
}
}
});
//点击读按钮触发读入事件
readButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
readContent.setText("");
FileInputStream fis = null;
try {
fis = openFileInput("fish");
if (fis.available() == 0) {
return;
} else {
byte[] con =new byte[fis.available()];
while (fis.read(con) != -1) {
}
readContent.setText(new String(con));
showContent.setText("读取成功");
}
} catch (FileNotFoundException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
}
});
}
}
- android中file的使用实例
- Android中使用File的时候File.separator的用法
- Android中使用AndroidTestCase的方法实例
- Android中Mvp的使用实例
- android中file的保存
- 【Android】Android中AlertDialog对话框的使用实例
- Android中 PopupWindow 使用实例
- Android中Handler使用实例
- Android中抽屉(SlidingDrawer)的使用介绍及实例记录
- Android中Activity的使用,简单实例讲解
- 实例讲解Android中AsyncTask的使用详解
- js中获取file控件的完整路径实例
- android file存储方法实例
- android file 文件存储实例
- jQuery-file-upload插件的使用(小实例)
- java.io.file使用实例
- [Android实例] Android中使用Google 地图实例详解
- Android中数据的存储之--------File
- Android之startActivityForResult的使用
- listview、gridview的setOnScrollListener使用(个人笔记)
- 工科生纠结的问题:为什么要学习程序设计
- WPF模板的学习
- MyGUI_Orge官网教程_7.使用按钮图像皮肤ButtonImage Skin
- android中file的使用实例
- FusionCharts的中文乱码解决
- 关于小波变换的一些理解
- 目前计划(希望朋友们能给出宝贵的建议,谢谢)
- 安卓相对布局
- JAVA继承中的构造函数
- tomcat中server.xml配置详解
- 计算机视觉+图像处理的好的链接网站!不断更新中!!!!
- ICE协议看明白