1,文件保存数据
输出流:OpenFileOutput
输入流:OpenFileInput
2,将数据报讯到内存卡里面
操作内存卡需要一下三个权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />挂载权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />写权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />读取权限
//获得外部存储卡
//File file = Environment.getExternalStorageDirectory();
//获取制定文件sdcard位置的目录
File file =Environment.getExternalStoragePublicDirectory("DCIM");
3,android工程中获取资源的引用;
使用文件的方式保存数据
1,创建布局文件 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_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" > <EditText android:id="@+id/text_v1" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center_horizontal" android:hint="输入框" /> <Button android:id="@+id/Save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text_v1" android:onClick="show" android:text="保 存" /> <Button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/Save" android:layout_below="@id/text_v1" android:onClick="show" android:text="清除输入框" /> <Button android:id="@+id/Read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/Save" android:onClick="show" android:text=" 读 取 " /> </RelativeLayout>
FileDemo操作数据,并显示
public class FileDemo extends Activity { private EditText text_v1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text_v1=(EditText)this.findViewById(R.id.text_v1); } // 按钮的监听器方法 public void show(View view) { switch (view.getId()) { case R.id.Save:// 保存 try { //将数据写到文件中 //参数一:文件名 //参数二:文件创建的模式;私有模式 FileOutputStream fos=this.openFileOutput("FileDemo", MODE_PRIVATE); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject("保存成内部文件的方式操作"); oos.flush(); oos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); break; case R.id.Read: //读取保存的文件的数据 try { //读取文件的数据 FileInputStream fis=this.openFileInput("FileDemo"); ObjectInputStream ois =new ObjectInputStream(fis); String str =(String)ois.readObject(); text_v1.setText(str); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case R.id.clear: //清空 text_v1.setText(""); break; } } }
数据保存到内存卡 关键代码
1,创建布局文件acivity_main.xml ,
<Button android:id="@+id/Save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text_v1" android:onClick="show" android:text="保 存" />
操作布局的文件
// 按钮的监听器方法 public void show(View view) { switch (view.getId()) { case R.id.Save:// 保存 //获得外部存储卡 //File file = Environment.getExternalStorageDirectory(); //获得制定sdcard位置的目录 File file =Environment.getExternalStoragePublicDirectory("DCIM"); //在DCIM的目录下创建文件C++++++++++++++ File files= new File(file,"C++++++++++++++"); if(!files.exists()){ Boolean b=file.mkdir(); if(!b){ Toast.makeText(this, "不能创建", Toast.LENGTH_SHORT).show(); } } if(!files.canWrite()){ Toast.makeText(this, "不能写文件", Toast.LENGTH_SHORT).show(); } //在C++++++++++++++的目录下创建文件abc File file1 = new File(files,"abc"); try { FileOutputStream fos = new FileOutputStream(file1); fos.write("长沙".getBytes()); fos.flush(); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); break; }
3,android工程中使用静态资源的方式
读取assets和res文件下的raw资源
text.txt文件的内容
raw文件下的text1.txt
运行效果:
详细代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_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" > <EditText android:id="@+id/text_v1" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center_horizontal" android:hint="输入框" /> <Button android:id="@+id/Save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text_v1" android:onClick="show" android:text="保 存" /> <Button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/Save" android:layout_below="@id/text_v1" android:onClick="show" android:text="清除输入框" /> <Button android:id="@+id/Read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/Save" android:onClick="show" android:text=" 读 取 " /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/Read" android:layout_below="@+id/Read" android:layout_marginLeft="56dp" android:layout_marginTop="154dp" android:ems="10" android:inputType="textMultiLine"/> </RelativeLayout>
Dmo操作数据的显示,读取assets和res文件下的raw资源
public class Demo extends Activity{ private ImageView img; private EditText text_v1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img=(ImageView)this.findViewById(R.id.imageView1); text_v1=(EditText)this.findViewById(R.id.text_v1); } public void show(View v){ switch (v.getId()) { case R.id.Save: //读取assets资源文件中的文件,使用流读取 try { //使用流读取Assets中的资源 InputStream is =this.getResources().getAssets().open("text.txt"); byte[] by= new byte[is.available()]; is.read(by); String str = EncodingUtils.getString(by,"GBK"); text_v1.setText(str); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case R.id.Read: //读取assets资源文件中的图片 try { InputStream ism =this.getResources().getAssets().open("img_1.jpg"); byte[] bs = new byte[ism.available()]; ism.read(bs); //将荼毒到的字节转化成bmp格式的图片 Bitmap bmp=BitmapFactory.decodeByteArray(bs, 0, bs.length); //设置图片显示 img.setImageBitmap(bmp); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } break; case R.id.clear: //res文件中创建的raw文件下的文件 InputStream is1=this.getResources().openRawResource(R.raw.text1); byte[] by; try { by = new byte[is1.available()]; is1.read(by); String str1 = EncodingUtils.getString(by,"GBK"); text_v1.setText(str1); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } } }
相关推荐
SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN, "Microsoft Access Driver (*.mdb)\0",cpConfig))//设置odbc数据源 步骤四: 设置Clistctrl控件的风格,为他加入图象资源。 首先要在CDemo1Dlg中加入#include...
事务控制语言(Transactional Control Language,TCL),用于维护数据的一致性,包括COMMIT(提交事务)、ROLLBACK(回滚事务)和SAVEPOINT(设置保存点)3条语句 二、 Oracle的数据类型 类型 参数 描述 字符类型...
将所有的源程序和头文件都加入到工程内,保存工程。 这时,我们可以尝试编译一下工程,你会得到许多错误提示信息,其内容无非是找不到osipparser2/xxxxx.h头文件之类。 处理:在Linux下,我们一般是将头文件...
例如,当我们点一个数据库的表,所有和此表相关的索引、约束、存储过程、SQL语句以及和其他表的相互引用关系都在同一界面显示出来。为了简化操作,用户可以在浏览窗口操作数据库对象。 SQL 编辑器: SQL 编辑器的...
接口类问题(B类)-指设计、编码中出现的函数和环境、其他函数、全局/局部变量或数据变量之间的数据/控制传输不匹配的问题,在系统中起重要作用,将导致模块间配合失效等严重问题; 维护类问题(C类)-指设计、...
� 计算技术、无线接入技术的发展,使嵌入式系统逐渐有能力对桌面系统常规业务进行支持。 � 谷歌长期以来奉行的移动发展战略:通过与全球各地的手机制造商和移动运营商结成合作伙伴,开发 既 有用又有吸引力的移动...
内容=(子元素|字符数据|字符数据段|引用|处理指令|注释)* 字符数据 字符数据中,不能含有&,<,>,',",需要采用实体引用的方式 字符数据段 <![CDATA[字符数据]]> 引用 <!ENTITY 引用名 "........
本书共分4部分,从xml、servlet、jsp和应用的角度向读者展示了java web开发中各种技术的应用,循序渐进地引导读者快速掌握java web开发。. 本书内容全面,涵盖了从事java web开发所应掌握的所有知识。在知识的讲解...
本书共分4部分,从xml、servlet、jsp和应用的角度向读者展示了java web开发中各种技术的应用,循序渐进地引导读者快速掌握java web开发。. 本书内容全面,涵盖了从事java web开发所应掌握的所有知识。在知识的讲解...
本书共分4部分,从xml、servlet、jsp和应用的角度向读者展示了java web开发中各种技术的应用,循序渐进地引导读者快速掌握java web开发。. 本书内容全面,涵盖了从事java web开发所应掌握的所有知识。在知识的讲解...
本书共分4部分,从xml、servlet、jsp和应用的角度向读者展示了java web开发中各种技术的应用,循序渐进地引导读者快速掌握java web开发。. 本书内容全面,涵盖了从事java web开发所应掌握的所有知识。在知识的讲解...
委托是一个数据结构,该数据结构引用一个静态方法,或引用一个对象实例和该对象的实例方法。在 C 或 C 中与委托最接近的是函数指针,但函数指针只能引用静态函数,而委托可以同时引用静态方法和实例方法。在后一种...
4 <br>0008 为程序设置版本和帮助信息 4 <br>0009 设置Windows应用程序启动窗体 5 <br>0010 设置Web应用程序起始页 5 <br>0011 如何设置程序的出错窗口 5 <br>0012 如何进行程序调试 6 ...
// 关闭TCP连接并释放与其关联的所有资源 user.commandSession.Close(); return; } else { switch (user.loginOK) { // 等待用户输入用户名: case 0: CommandUser(user, command, param); break; // ...
根据Netcraft在1999年10月的报告,有931122个域和321128个IP地址利用PHP技术。 1.2 PHP的先进之处 应用PHP有许多好处。当然已知的不利之处在于PHP由于是开放源码项目,没有什么商业支持,并且由此而带来的执行速度...
2.3.1. 保存 ACL 数据确保持久性 2.3.2. 使用声明(Assert)来编写条件性的 ACL 规则 3. Zend_Auth 3.1. 简介 3.1.1. 适配器 3.1.2. 结果 3.1.3. 身份的持久(Persistence) 3.1.3.1. 在PHP Session 中的缺省...
5.2. 导入数据到版本库 .................................................... 38 5.2.1. 导入 ......................................................... 38 5.2.2. 导入适当的位置 ..............................