django--python之web开发框架搭建
2012-08-11 11:11
981 查看
在Android中,我们可以将一些数据直接以文件的形式保存在设备中。例如:一些文本文件、PDF文件、音视频文件和图片等。Android 提供了文件读写的方法。
通过 Context.openFileInput()方法获得标准Java文件输入流(FileInputStream),通过Context.openFileOutput()方法获得标准Java文件输出流( FileOutputStream )。使用
Resources.openRawResource(R.raw.myDataFile)方法返回InputStream。
示例如下,新建一个Activity,添加两个TextView和两个Button,点击第一个Button,将TextView上的数据写到文件中,点击第二个Button,将文件中的数据写到TextView中。
MainActivity.java:
activity_main.xml:
运行结果如下: 点击write将第一个TextView写入文件,点击read将数据读出到第二个TextView
本文出自 “无用大叔” 博客,请务必保留此出处http://aslonely.blog.51cto.com/6552465/1618819
通过 Context.openFileInput()方法获得标准Java文件输入流(FileInputStream),通过Context.openFileOutput()方法获得标准Java文件输出流( FileOutputStream )。使用
Resources.openRawResource(R.raw.myDataFile)方法返回InputStream。
示例如下,新建一个Activity,添加两个TextView和两个Button,点击第一个Button,将TextView上的数据写到文件中,点击第二个Button,将文件中的数据写到TextView中。
MainActivity.java:
public class MainActivity extends Activity { private String filename = "file.txt"; private TextView mytext1,mytext2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mytext1=(TextView)this.findViewById(R.id.text1); mytext2=(TextView)this.findViewById(R.id.text2); Button button1=(Button)this.findViewById(R.id.btn_read); Button button2=(Button)this.findViewById(R.id.btn_write); button1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub mytext2.setText(read()); } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub write(mytext1.getText().toString()); } }); } protected String read(){ try { FileInputStream fis = openFileInput(filename); try { byte[] buffer = new byte[fis.available()]; fis.read(buffer); return new String(buffer); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } protected void write(String str) { try { FileOutputStream fos = openFileOutput(filename,MODE_APPEND); try { fos.write(str.getBytes()); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我是text1"/> <Button android:id="@+id/btn_write" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="write"/> <TextView android:id="@+id/text2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我是text2"/> <Button android:id="@+id/btn_read" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="read"/> </LinearLayout>
运行结果如下: 点击write将第一个TextView写入文件,点击read将数据读出到第二个TextView
本文出自 “无用大叔” 博客,请务必保留此出处http://aslonely.blog.51cto.com/6552465/1618819
相关文章推荐
- PythonWeb 开发Django框架(一) 4000 环境搭建
- Python之Web框架Django项目搭建全过程
- Python Web 框架,第 1 部分: 使用 Django 和 Python 开发 Web 站点
- Eclipse+PyDev+Django+Mysql搭建Python web开发环境
- python开发学习-day15(前端部分知识、web框架、Django创建项目)
- Python中使用django框架进行web开发,模板中无法导入css、js和图片的问题解决
- 全面解读Python Web开发框架Django
- 每周一荐:Python Web开发框架Django
- Python & PyCharm & Django 搭建web开发环境
- python Web开发框架-Django (1)
- python +Django 搭建web开发环境初步,显示当前时间
- python下的web开发框架-Django,django模板的使用
- python Web开发框架-Django (1)
- Python web框架Django学习(1)——在win7 64bit下配置开发环境Django:一个可以使Web开发工作愉快并且高效的Web开发框架。 使用Django,使你能够以最小的代价构建和
- python-django开发web框架的setting.py
- Python Web开发之Django环境搭建
- Python使用django搭建web开发环境
- 基于Flask框架的Python web程序的开发实战 <一> 环境搭建
- Python Web 框架,第 1 部分: 使用 Django 和 Python 开发 Web 站点
- Eclipse+PyDev+Django+Mysql搭建Python web开发环境