Android开发实现Files文件读取解析功能示例
更新时间:2017年09月08日 10:17:42 作者:ITzhongzi
这篇文章主要介绍了Android开发实现Files文件读取解析功能,结合实例形式分析了Android针对txt文本文件的读取、保存功能实现方法与布局操作技巧,需要的朋友可以参考下
本文实例讲述了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程序设计有所帮助。
您可能感兴趣的文章:
- Android学习笔记-保存文件(Saving Files)
- android开发教程之获取power_profile.xml文件的方法(android运行时能耗值)
- Android编程实现文件浏览功能的方法【类似于FileDialog的功能】
- Android文件选择器ExFilePicker的使用方法
- Android第三方文件选择器aFileChooser使用方法详解
- Android中文件读写(输入流和输出流)操作小结
- Android编程之文件的读写实例详解
- Android 读写文件方法汇总
- Android编程之文件读写操作与技巧总结【经典收藏】
- Android持久化技术之文件的读取与写入实例详解
- Android编程中File文件常见存储与读取操作demo示例
相关文章
Android ViewPagerIndicator详解及实例代码
这篇文章主要介绍了Android ViewPagerIndicator详解及实例代码的相关资料,需要的朋友可以参考下2017-05-05
flutter BottomAppBar实现不规则底部导航栏
这篇文章主要为大家详细介绍了flutter BottomAppBar实现不规则底部导航栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2019-07-07
Android中TabLayout+ViewPager实现tab和页面联动效果
本篇文章主要介绍了Android中TabLayout+ViewPager实现tab和页面联动效果,具有一定的参考价值,有兴趣的可以了解一下2017-06-06


最新评论