您的位置:首页 > 移动开发 > Android开发

android文件的创建,删除和输入,输出操作

2017-01-16 12:39 393 查看
我的博客:http://blog.csdn.net/wanxuedong

前言:没有

我这人就不喜欢多解释,但是我会在注释里写好,这样也就方便你们看也方便我写了。这个程序主要讲怎么在代码里面创建文件和往文件里面添加数据的内容,解释的比较详细,小白应该都可以看懂。

先给出几个图片来吊吊大家胃口:













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 {

4000
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>


最后同学们不要忘了加上权限,这里也给了:

<!--往sdcard中写入数据的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--在sdcard中创建/删除文件的权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>


结束语:代码里面的解释应该是比较清楚的,不会的同学可以直接问我,QQ2381144912,最后快到过年了,这里就先祝大家新年快乐了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  注释 数据
相关文章推荐