Android開發(fā)實(shí)現(xiàn)Files文件讀取解析功能示例
本文實(shí)例講述了Android開發(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開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- Android學(xué)習(xí)筆記-保存文件(Saving Files)
- android開發(fā)教程之獲取power_profile.xml文件的方法(android運(yùn)行時(shí)能耗值)
- Android編程實(shí)現(xiàn)文件瀏覽功能的方法【類似于FileDialog的功能】
- Android文件選擇器ExFilePicker的使用方法
- Android第三方文件選擇器aFileChooser使用方法詳解
- Android中文件讀寫(輸入流和輸出流)操作小結(jié)
- Android編程之文件的讀寫實(shí)例詳解
- Android 讀寫文件方法匯總
- Android編程之文件讀寫操作與技巧總結(jié)【經(jīng)典收藏】
- Android持久化技術(shù)之文件的讀取與寫入實(shí)例詳解
- Android編程中File文件常見存儲(chǔ)與讀取操作demo示例
相關(guān)文章
Android監(jiān)聽輸入法彈窗和關(guān)閉的實(shí)現(xiàn)方法
用過ios的都知道ios上輸入法關(guān)閉的同時(shí)會(huì)自動(dòng)關(guān)閉輸入框,那么在android上如何實(shí)現(xiàn)監(jiān)聽輸入法彈出和關(guān)閉呢?接下來通過本文給大家分享一種可靠的實(shí)現(xiàn)方式2016-11-11解決genymotion模擬器無法聯(lián)網(wǎng)的正確方法100%成功
android 5.1版不能聯(lián)網(wǎng),三個(gè)步驟的設(shè)置就可以解決你的genymotion模擬器無法聯(lián)網(wǎng)的問題2018-03-03Kotlin中的惰性操作容器Sequence序列使用原理詳解
這篇文章主要為大家介紹了Kotlin中的惰性操作容器Sequence序列使用原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android實(shí)現(xiàn)將應(yīng)用崩潰信息發(fā)送給開發(fā)者并重啟應(yīng)用的方法
這篇文章主要介紹了Android實(shí)現(xiàn)將應(yīng)用崩潰信息發(fā)送給開發(fā)者并重啟應(yīng)用的方法,涉及Android錯(cuò)誤處理與應(yīng)用操作的相關(guān)技巧,需要的朋友可以參考下2016-03-03Android apk 插件啟動(dòng)內(nèi)存釋放問題
這篇文章主要介紹了Android apk 插件啟動(dòng)內(nèi)存釋放問題的相關(guān)資料,需要的朋友可以參考下2017-06-06android微信授權(quán)獲取用戶個(gè)人信息代碼
大家好,本篇文章主要講的是android微信授權(quán)獲取用戶個(gè)人信息代碼,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12圖解Windows環(huán)境下Android Studio安裝和使用教程
這篇文章主要介紹了圖解Windows環(huán)境下Android Studio安裝和使用教程的相關(guān)資料,需要的朋友可以參考下2015-12-12