Android開(kāi)發(fā)實(shí)現(xiàn)Files文件讀取解析功能示例
本文實(shí)例講述了Android開(kāi)發(fā)實(shí)現(xiàn)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());
}
});
}
//保存文件內(nèi)容
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>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android學(xué)習(xí)筆記-保存文件(Saving Files)
- android開(kāi)發(fā)教程之獲取power_profile.xml文件的方法(android運(yùn)行時(shí)能耗值)
- Android編程實(shí)現(xiàn)文件瀏覽功能的方法【類似于FileDialog的功能】
- Android文件選擇器ExFilePicker的使用方法
- Android第三方文件選擇器aFileChooser使用方法詳解
- Android中文件讀寫(xiě)(輸入流和輸出流)操作小結(jié)
- Android編程之文件的讀寫(xiě)實(shí)例詳解
- Android 讀寫(xiě)文件方法匯總
- Android編程之文件讀寫(xiě)操作與技巧總結(jié)【經(jīng)典收藏】
- Android持久化技術(shù)之文件的讀取與寫(xiě)入實(shí)例詳解
- Android編程中File文件常見(jiàn)存儲(chǔ)與讀取操作demo示例
相關(guān)文章
Android開(kāi)發(fā)實(shí)現(xiàn)生成excel的方法詳解
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)生成excel的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android生成Excel的具體步驟與存儲(chǔ)、導(dǎo)入、添加等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Android ViewPagerIndicator詳解及實(shí)例代碼
這篇文章主要介紹了Android ViewPagerIndicator詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
flutter BottomAppBar實(shí)現(xiàn)不規(guī)則底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了flutter BottomAppBar實(shí)現(xiàn)不規(guī)則底部導(dǎo)航欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Android中TabLayout+ViewPager實(shí)現(xiàn)tab和頁(yè)面聯(lián)動(dòng)效果
本篇文章主要介紹了Android中TabLayout+ViewPager實(shí)現(xiàn)tab和頁(yè)面聯(lián)動(dòng)效果,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06

