本文實(shí)例講述了Android開(kāi)發(fā)實(shí)現(xiàn)Files文件讀取解析功能。分享給大家供大家參考,具體如下:
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),宿州企業(yè)網(wǎng)站建設(shè),宿州品牌網(wǎng)站建設(shè),網(wǎng)站定制,宿州網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,宿州網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。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; } }