文件存取编程基础

文件

   1. 文件可以用来存储比使用引用更大数量的数据
   2. Android 提供方法来读、写文件
   3. 只有本地文件可以被访问
   4. 优点:可以存储大容量的数据
   5. 缺点:文件更新或是格式改变可能会导致巨大的编程工作

文件操作
## 读文件 ##
Context.openFileInput(String name)打开一个与应用程序联系的私有文件输入流
当文件不存在时抛出FileNotFoundException 异常

FileInputStream in = this.openFileInput("test2.txt");//打开文件"test2.txt"
……
in.close();//关闭输入流

`## 写文件 ##“
Context.openFileOutput(String name,int mode)开启一个与应用程序联系的私有文件输出流
当文件不存在时该文件将被创建
文件输出流可以在添加模式中打开,这意味新的数据将被添加到文件的末尾

FileOutputStream out = this.openFileOutput(“test2.txt”,MODE_APPEND);
//打开文件”test2.txt”进行写操作、使用MODE_APPEND 在添加模式中打开文件
……
out.close();//关闭输出流

**读取静态文件**
      要打开打包在应用程序中的静态文件,使用Resources.openRawResource(R.raw.mydatafile)
      该文件必须放在文件夹res/raw/中
      ![这里写图片描述](http://img.blog.csdn.net/20160621153148472)

InputStreamin = this.getResources().openRawResource(R.raw.my);
… //获得Context资源
in.close();//关闭输入流

**文件存取示例**
创建添加文件内容并保存,打开文件并显示内容
① 新建工程FileWriteRead
② 修改main.xml 布局,添加一个EditText、一个Button

③ 在res/layout 中新建一个open.xml 布局文件,添加一个TextView、两个Button("打开","清空")

④ 文件存储

/定义IO对象/
private String Text_of_input;
private OutputStream os;
Text_of_input = inputArt.getText().toString();
//得到用户输入字符
try {
os = this.openFileOutput(“txtME”, MODE_PRIVATE);
//打开一个文件输出流。名称为txtME,模式为不覆盖
os.write(Text_of_input.getBytes());
//把内容写入文件
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
} finally {
try {
//关闭文件输出流
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
}

⑤ 文件读取

/定义IO对象/
private String Text_of_output;
private InputStream is;
private byte[] b;
try {
//打开一个文件输入流。名称为txtME
is = this.openFileInput(“txtME”);
//字节数组声明定义
b = new byte[1024];
//读取文件内容放入字节数组
int length = is.read(b);
//把字节数组转换成字符串
Text_of_output = new String(b);
//显示读取内容长度
setTitle(“文件字数:” + length);
//显示读取的文件内容
showmyText.setText(Text_of_output);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}finally {
try {
//关闭文件输入流
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
}

⑥ 修改mianActivity.java 文件,添加menu 菜单与操作

package zyf.FileWrite;
/导入要使用的包/
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class FileWrite extends Activity implements Button.OnClickListener {
/* Called when the activity is first created. /
/要使用的对象、变量声明/
/保存部分/
private EditText inputArt;/编辑框,输入用户字符串/
private Button saveButton;/按钮,保存/
private String Text_of_input;/字符串,用户输入的字符串/
private OutputStream os;/文件输出流,保存文件流/
/读取部分/
private TextView showmyText;/TextView,显示读取文件内容/
private Button openTxt,cleanTxt;/按钮,打开文件/
private String Text_of_output;/字符串,从文件中读取到得 字符串/
private InputStream is;/文件输入流,读取文件流/
private byte[] b;/字节数组,用来读取文件内容/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setLayoutShow(R.layout.main);/设置主屏布局/
UIinit(“main”);/初始化UI元素方法/
Logic(“main”);/添加事件逻辑方法/
} /*设



*/
private void setLayoutShow(int layoutID) {
// TODO Auto-generated method stub
setContentView(layoutID);/设置当前主屏布局/
} private void UIinit(String mainROopen) {
/初始化UI/
if (mainROopen.equals(“main”)) {
inputArt = (EditText) findViewById(R.id.EditText_Txt);
saveButton = (Button) findViewById(R.id.Button_Save);
} else if (mainROopen.equals(“open”)) {
showmyText = (TextView) findViewById(R.id.TextView_showTxt);
openTxt = (Button) findViewById(R.id.Button_openTxt);
cleanTxt=(Button)findViewById(R.id.Button_clean);
}
} private void Logic(String string) {
// TODO Auto-generated method stub
/为按钮添加事件处理/
if (string.equals(“main”)) {
saveButton.setOnClickListener(this);
} else if (string.equals(“open”)) {
openTxt.setOnClickListener(this);
cleanTxt.setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
/根据ID判断按钮事件/
switch (v.getId()) {
case R.id.Button_Save: {
/提示/
NoteDebug(“文件保存”);
// TODO Auto-generated method stub
/获得用户输入的字符串/
Text_of_input = inputArt.getText().toString();
try {
/打开文件输出流,名称txtME,以不覆盖模式打开/
os = this.openFileOutput(“txtME”, MODE_PRIVATE);
/把字符串转换成字节数组,写入文件中/
os.write(Text_of_input.getBytes());
} catch (FileNotFoundException e) {
/文件未找到,异常/
// TODO Auto-generated catch block
NoteDebug(“文件关闭失败” + e);
} catch (IOException e) {
/文件写入错误/
// TODO Auto-generated catch block
NoteDebug(“文件写入失败” + e);
} finally {
try {
/关闭文件输出流/
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
NoteDebug(“文件关闭失败” + e);
}
} /*输




*/
inputArt.setText(“”);
}
break;
case R.id.Button_openTxt: {
NoteDebug(“文件打开”);
try {
/打开文件输入流,名称txtME/
is = this.openFileInput(“txtME”);
/初始化字节数组/
b = new byte[1024];
/从文件输入流中读取内容到字节数组中,返回内容长度/
int length = is.read(b);
/把字节数组转换成字符串/
Text_of_output = new String(b);
/设置标题,显示文件内容长度/
setTitle(“文件字数:” + length);
/显示文件内容/
showmyText.setText(Text_of_output);
} catch (FileNotFoundException e) {
/文件未找到,异常/
// TODO Auto-generated catch block
NoteDebug(“文件打开失败” + e);
} catch (IOException e) {
/文件读取错误,异常/
// TODO Auto-generated catch block
NoteDebug(“文件读取失败” + e);
}
finally {
try {
/关闭文件输入流/
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
NoteDebug( “文件关闭失败”+e);
}
}
}
break;
case R.id.Button_clean:{
/清空/
showmyText.setText(“”);
NoteDebug(“清空”);
}
break;
default:
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
/添加三个菜单项目,并设置图片/
menu.add(0, 1, 1, “Edit”).setIcon(R.drawable.ic_menu_edit);
menu.add(0, 2, 2, “Open”).setIcon(R.drawable.ic_menu_agenda);
menu.add(0, 3, 3, “Exit”).setIcon(R.drawable.ic_lock_power_off);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case 1:
/显示main.xml为主屏布局/
setLayoutShow(R.layout.main);
UIinit(“main”);
Logic(“main”);
NoteDebug(“编辑文件Layout”);
break;
case 2:
/显示open.xml为主屏布局/
setLayoutShow(R.layout.open);
UIinit(“open”);
Logic(“open”);
NoteDebug( “打开文件Layout”);
break;
case 3:
/退出/
finish();
NoteDebug( “Byebye”);
break;
default:
break;
} return super.onOptionsItemSelected(item);
} private void NoteDebug(String showString){
/显示Toast提示/
Toast.makeText(this,showString, Toast.LENGTH_SHORT).show();
}
}
“`
⑦ 结果
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

发布了37 篇原创文章 · 获赞 7 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章