Android开发实现Files文件读取解析功能示例

发布时间:2020-10-10 00:00:11 作者:ITzhongzi
来源:脚本之家 阅读:178

本文实例讲述了Android开发实现Files文件读取解析功能。分享给大家供大家参考,具体如下:

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程序设计有所帮助。

推荐阅读:
  1. Android开发实现实现缓存功能
  2. python如何从文件读取数据及解析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android 文件 les

上一篇:python原类、类的创建过程与方法详解

下一篇:Java 选择排序、插入排序、希尔算法实例详解

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》