Android Files文件读取解析 Android开发实现Files文件读取解析功能示例
ITzhongzi 人气:0想了解Android开发实现Files文件读取解析功能示例的相关内容吗,ITzhongzi在本文为您仔细讲解Android Files文件读取解析的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,Files,文件,读取,解析,下面大家一起来学习吧。
本文实例讲述了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程序设计有所帮助。
加载全部内容