个人博客:http://blog.csdn.net/wanxuedongjava
前言:没有android
我这人就不喜欢多解释,可是我会在注释里写好,这样也就方便大家看也方便我写了。这个程序主要讲怎么在代码里面建立文件和往文件里面添加数据的内容,解释的比较详细,小白应该均可以看懂。
先给出几个图片来吊吊你们胃口:
web
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends Activity implements View.OnClickListener {
private static File file;//咱们运行这个程序须要建立的文件
private String filename = "newfile";//咱们这个程序运行的文件的文件名
private String str = null;//咱们往文件里面输入的数据,等会从edittext里面获取
private EditText edittext;//咱们输入数据,以便存储
private Button sure;//打开文件,直接录入数据,但会删除以前的数据
private Button add;//往文件里面追加数据,意思机会会保存以前的数据
private TextView textView;//最后咱们从文件里读取数据并显示在这里
private Button create;//建立文件的按钮
private Button delete;//删除文件的按钮
private Button load;//读取数据的按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initValue();
initEvent();
}
/*初始化一些数据,赋值*/
private void initValue() {
/*获取该程序所在sd卡的路径,并加上文件名,记住这里的这个 "/" 绝对不能省,我就是没写,调了我半天,如今大家该知道了, /若是不写,你建立的文件名就会和系统目录的文件名混到一块儿,而后下面的几个操做所有都会提示找不到你建立的这个文件*/
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + filename);
edittext = (EditText) findViewById(R.id.put);
sure = (Button) findViewById(R.id.sure);
add = (Button) findViewById(R.id.add);
textView = (TextView) findViewById(R.id.get);
delete = (Button) findViewById(R.id.delete);
create = (Button) findViewById(R.id.create);
load = (Button) findViewById(R.id.load);
}
/*初始化一些相应事件*/
private void initEvent() {
sure.setOnClickListener(this);
add.setOnClickListener(this);
delete.setOnClickListener(this);
create.setOnClickListener(this);
load.setOnClickListener(this);
}
@Override
public void onClick(View view) {
str = edittext.getText().toString();
switch (view.getId()) {
case R.id.create:
//检查该文件夹是否存在,不存在则从新建立
if (!file.exists()) {
try{
file.mkdir();//关键的一步,建立以上路径下的该文件夹
Toast.makeText(this, "文件建立成功!", Toast.LENGTH_SHORT).show();
}catch (Exception E)
{
E.printStackTrace();
}
} else {
Toast.makeText(this, "文件已存在,无需重复建立", Toast.LENGTH_SHORT).show();
}
break;
case R.id.sure:
writefile(str);
break;
case R.id.add:
addtofile(str);
break;
case R.id.delete:
if (file.exists()) {
file.delete();
Toast.makeText(this, "文件删除成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "文件不存在,无需删除", Toast.LENGTH_SHORT).show();
}
break;
case R.id.load:
textView.setText(readfile());//将读取的数据显示出来
break;
default:
break;
}
}
/*往文件里面录入数据*/
private void writefile(String str) {
if (file.exists()) {
try {
/*以流的形式打开该文件,并设置了保存模式* /总共有四种模式: /Context.MODE_PRIVATE 为默认操做模式,表明该文件是私有数据,只能被应用自己访问,在该模式下写入的内容会覆盖原文件的内容。 /Context.MODE_APPEND 检查文件是否存在,存在就往文件追加内容,不然就建立新文件。 /MODE_WORLD_READABLE 表示当前文件能够被其余应用读取。 /MODE_WORLD_WRITEABLE 表示当前文件能够被其余应用写入 */
FileOutputStream outstream = openFileOutput(filename, MODE_PRIVATE);
//将文字以字节的形式存储进文件
outstream.write(str.getBytes());
//关闭文件存储流
outstream.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "文件不存在,没法写入数据", Toast.LENGTH_SHORT).show();
}
}
/*往文件里面追加数据*/
private void addtofile(String str) {
if (file.exists()) {
try {
//这里和上面只改了保存模式,效果就彻底不一样了
FileOutputStream outstream = openFileOutput(filename, MODE_APPEND);
//将文字以字节的形式存储进文件
outstream.write(str.getBytes());
//关闭文件存储流
outstream.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "文件不存在,没法追加数据", Toast.LENGTH_SHORT).show();
}
}
/*从文件里面读取数据*/
private String readfile() {
String content = null;
if (file.exists()) {
try {
//以流的形式打开该文件
FileInputStream instream = openFileInput(filename);
//初始化一个字数组节流,等下将以前存入的数据已流的形式读取出来,再转成字符串便可
ByteArrayOutputStream Byte = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = instream.read(buffer)) != -1) {
Byte.write(buffer, 0, len);
}
content = Byte.toString();
//关闭流
instream.close();
//关闭字节数组流
Byte.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "文件不存在,没法读取数据", Toast.LENGTH_SHORT).show();
content = "无可显示数据";
}
//返回从文件得到的数据
return content;
}
}
这里再给出布局代码吧,方便小伙伴们直接粘贴复制了。数组
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<EditText android:id="@+id/put" android:layout_width="match_parent" android:layout_height="wrap_content" />
<Button android:id="@+id/create" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="建立文件" />
<Button android:id="@+id/sure" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="输入" />
<Button android:id="@+id/add" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="追加" />
<Button android:id="@+id/load" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="读取数据" />
<Button android:id="@+id/delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除文件" />
<TextView android:id="@+id/get" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示数据的地方" android:textSize="20dp" />
</LinearLayout>
最后同窗们不要忘了加上权限,这里也给了:app
<!--往sdcard中写入数据的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--在sdcard中建立/删除文件的权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
结束语:代码里面的解释应该是比较清楚的,不会的同窗能够直接问我,QQ2381144912,最后快到过年了,这里就先祝你们新年快乐了。ide