android 文件系统操作 文件读取与写入
2014-04-21 10:38
381 查看
android文件操作主要使用java io api
布局文件:
程序主文件:
文件操作业务逻辑类
简单文件读取:
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:orientation="vertical" > <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:minLines="3" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存文件" ></Button> <Button android:id="@+id/readbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取文件" /> <EditText android:id="@+id/readname" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" /> <TextView android:id="@+id/neirongxianshi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> " " </LinearLayout> </RelativeLayout>
程序主文件:
package com.example.mytool; import com.example.service.FileService; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private EditText mytext;//寻找空间这种事情只需要做一次就够了 private EditText duanxin; private EditText mfilename; private TextView xianshinr; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mytext = (EditText) findViewById(R.id.editText1);//得到文本输入框 duanxin=(EditText)this.findViewById(R.id.editText2); Button button = (Button) this.findViewById(R.id.button1);//根据id寻找控件 button.setOnClickListener(new ButtonClick());//添加点击事件 Button readbtn=(Button)this.findViewById(R.id.readbtn); readbtn.setOnClickListener(new Mybtnclick()); mfilename=(EditText)this.findViewById(R.id.readname); xianshinr=(TextView)this.findViewById(R.id.neirongxianshi); } private final class Mybtnclick implements View.OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub FileService duqu=new FileService(getApplicationContext()); String fname=mfilename.getText().toString(); try { String filestr=duqu.read(fname); xianshinr.setText(filestr); Toast.makeText(getApplicationContext(), "读取成功", Toast.LENGTH_LONG).show(); } catch (Exception e) { // TODO Auto-generated catch block Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_LONG).show(); e.printStackTrace(); } } } private final class ButtonClick implements View.OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub String mobileText = mytext.getText().toString(); String content=duanxin.getText().toString(); FileService oj=new FileService(getApplicationContext()); try{ oj.save(mobileText, content); Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG).show(); }catch(Exception e){ Toast.makeText(getApplicationContext(), "保存失败", Toast.LENGTH_LONG).show(); } } } @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; } }
文件操作业务逻辑类
package com.example.service; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import android.content.Context; public class FileService { private final Context context; public FileService(Context context) { this.context = context; } public void save(String filename,String content) throws Exception{ FileOutputStream infile=context.openFileOutput(filename, context.MODE_PRIVATE); infile.write(content.getBytes()); infile.close(); } public String read(String filaname) throws Exception{ FileInputStream rfile = context.openFileInput(filaname); ByteArrayOutputStream neicun = new ByteArrayOutputStream(); byte[] buf=new byte[1024]; int len=0; while((len=rfile.read(buf))!=-1){ neicun.write(buf, 0, len); } rfile.close(); neicun.close(); String restr=new String(buf,"GBK"); return restr; } }
简单文件读取:
InputStream infile=context.getAssets().open(fname); int len=infile.available(); byte[] str=new byte[len]; infile.read(str); infile.close(); return new String(str,"utf-8");
相关文章推荐
- 写入文件操作 之 信息管理系统 c语言版
- android系统的四种文件操作模式
- android系统外部存储文件写操作的具体实现
- Android入门之文件系统操作(二)文件操作相关指令
- [android开发]对象的持久化操作 (写入对象到本地文件, 从本地文件中读取对象)
- (转)linux sudo 重定向,实现只有系统管理员才有权限操作的文件中写入信息
- android OutputStream向sd卡中文件写入操作立即关机数据清0问题
- Android中的yaffs2文件系统与文件操作底层实现
- Android入门之文件系统操作(二)文件操作相关指令
- android 数据库技术 | 文件系统,SDCARD访问,CONTENT PROVIDER,SQLITE ,CURSOR操作,CURSOR适配器,SQLITEHELPER,数据库设计模式
- Linux、Windows直接操作android系统文件
- cordova操作Android本地文件系统
- Android对文件的操作(简单的文件读取与写入)
- 【android开发】文件系统操作
- 关于Android系统目录下文件的操作
- Android入门之文件系统操作(二)文件操作相关指令
- Mac系统下利用ADB命令连接android手机并进行文件操作
- Android入门之文件系统操作
- Android写入文件操作权限