Android开发实现Files文件读取解析功能示例
2017-09-08 10:17
846 查看
package com.example.file; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; 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.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity { EditText edt; Button btn; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edt = (EditText) findViewById(R.id.editText); btn = (Button) findViewById(R.id.button); tv = (TextView) findViewById(R.id.textView); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { WriteFiles(edt.getText().toString()); tv.setText(readFiles()); } }); } //保存文件内容 public void WriteFiles(String content){ try { FileOutputStream fos = openFileOutput("a.txt",MODE_PRIVATE); fos.write(content.getBytes()); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //读取文件 public String readFiles(){ String content = null; try { FileInputStream fis = openFileInput("a.txt"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[]buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer))!=-1) { baos.write(buffer,0,len); } content = baos.toString(); fis.close();; baos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return content; } }
<?xml version="1.0" encoding="utf-8"?> <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="com.example.file.MainActivity"> <EditText android:layout_width="wrap_content" android:layout_height="200dp" android:id="@+id/editText" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" android:layout_below="@+id/editText" android:layout_centerHorizontal="true" android:layout_marginTop="90dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/textView" android:layout_below="@+id/button" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout>
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android文件操作技巧汇总》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android布局layout技巧总结》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:
- Android学习笔记-保存文件(Saving Files)
- android开发教程之获取power_profile.xml文件的方法(android运行时能耗值)
- Android编程实现文件浏览功能的方法【类似于FileDialog的功能】
- Android文件选择器ExFilePicker的使用方法
- Android第三方文件选择器aFileChooser使用方法详解
- Android中文件读写(输入流和输出流)操作小结
- Android编程之文件的读写实例详解
- Android 读写文件方法汇总
- Android编程之文件读写操作与技巧总结【经典收藏】
- Android持久化技术之文件的读取与写入实例详解
- Android编程中File文件常见存储与读取操作demo示例
相关文章推荐
- PHP实现类似于C语言的文件读取及解析功能
- java实现简单解析XML文件功能示例
- java实现倒序读取文件功能示例分享
- Android实现文件的保存与读取功能示例
- JS基于FileSaver.js插件实现文件保存功能示例[原创]_javascript技巧_脚本之家
- JavaWeb实现文件上传下载功能实例解析以及项目工程示例下载
- 纯JS实现的读取excel文件内容功能示例【支持所有浏览器】
- IE下使用控件实现读取本地文件功能并显示
- 解析pcap文件及读取实现源码
- C语言实现的配置文件读取功能,喜欢的拿去用.
- php读取csv实现csv文件下载功能
- java中struts2实现文件上传下载功能实例解析
- Java 通过 BufferReader 实现 文件 写入读取 示例
- Spark读取HDFS文件,文件格式为GB2312,实现WordCount示例
- 读取txt文件实现搜索替换功能
- Java 通过 BufferReader 实现 文件 写入读取 示例
- Hpple-iOS解析HTML-hpple读取html文件内容的代码 更新了下拉刷新功能
- jQuery实现Ajax功能示例续 -- 读取dataset
- java使用dom4j解析xml配置文件实现抽象工厂反射示例
- 解析pcap文件及读取实现源码