日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

03_Android项目中读写文本文件的代码

發布時間:2024/9/27 Android 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 03_Android项目中读写文本文件的代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 編寫一下Android界面的項目

  • 使用默認的Android清單文件

  • <?xml version="1.0" encoding="utf-8"?>

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"

    ??? package="com.itheima28.writedata"

    ??? android:versionCode="1"

    ??? android:versionName="1.0" >

    ?

    ??? <uses-sdk

    ??????? android:minSdkVersion="8"

    ??????? android:targetSdkVersion="19" />

    ?

    ??? <application

    ??????? android:allowBackup="true"

    ??????? android:icon="@drawable/ic_launcher"

    ??????? android:label="@string/app_name"

    ??????? android:theme="@style/AppTheme" >

    ??????? <activity

    ??????????? android:name="com.itheima28.writedata.MainActivity"

    ??????????? android:label="@string/app_name" >

    ??????????? <intent-filter>

    ??????????????? <action android:name="android.intent.action.MAIN" />

    ?

    ??????????????? <category android:name="android.intent.category.LAUNCHER" />

    ??????????? </intent-filter>

    ??????? </activity>

    ??? </application>

    ?

    </manifest>

    ?

  • Android布局文件

  • <LinearLayout 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:orientation="vertical"

    ??? tools:context=".MainActivity">

    ?

    ??? <Button

    ??????? android:id="@+id/btn_read_private"

    ??????? android:layout_width="wrap_content"

    ??????? android:layout_height="wrap_content"

    ??????? android:text="讀私有文件" />

    ?

    ??? <Button

    ??????? android:id="@+id/btn_write_private"

    ??????? android:layout_width="wrap_content"

    ??????? android:layout_height="wrap_content"

    ??????? android:text="寫私有文件" />

    ?

    ??? <Button

    ??????? android:id="@+id/btn_read_readable"

    ??????? android:layout_width="wrap_content"

    ??????? android:layout_height="wrap_content"

    ??????? android:text="讀可讀文件" />

    ?

    ??? <Button

    ??????? android:id="@+id/btn_write_readable"

    ??????? android:layout_width="wrap_content"

    ??????? android:layout_height="wrap_content"

    ??????? android:text="寫可讀文件" />

    ?

    ??? <Button

    ??????? android:id="@+id/btn_read_writeable"

    ??????? android:layout_width="wrap_content"

    ??????? android:layout_height="wrap_content"

    ??????? android:text="讀可寫文件" />

    ?

    ??? <Button

    ??????? android:id="@+id/btn_write_writeable"

    ??????? android:layout_width="wrap_content"

    ??????? android:layout_height="wrap_content"

    ??????? android:text="寫可寫文件" />

    ?

    ??? <Button

    ??????? android:id="@+id/btn_read_readable_writeable"

    ??????? android:layout_width="wrap_content"

    ??????? android:layout_height="wrap_content"

    ??????? android:text="讀可讀可寫文件" />

    ?

    ??? <Button

    ??????? android:id="@+id/btn_write_readable_writeable"

    ??????? android:layout_width="wrap_content"

    ??????? android:layout_height="wrap_content"

    ??????? android:text="寫可讀可寫文件" />

    ?

    </LinearLayout>

    4 Android中的寫文本文件的代碼

    package com.itheima28.writedata;

    ?

    import java.io.BufferedReader;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.InputStreamReader;

    ?

    import android.content.Context;

    import android.os.Bundle;

    import android.support.v7.app.ActionBarActivity;

    import android.view.View;

    import android.view.View.OnClickListener;

    import android.widget.Toast;

    ?

    /**

    ?* 讀寫文件

    ?* 注意可以將寫文件和寫文件的兩個功能分別寫到不同的項目中進行測試

    ?* @author toto

    ?*/

    public class MainActivity extends ActionBarActivity implements OnClickListener{

    ??? //這個路徑是文件所在路徑

    ???????? private String basicPath = "/data/data/com.itheima28.writedata/files/";

    ????????

    ???????? @Override

    ???????? protected void onCreate(Bundle savedInstanceState) {

    ?????????????????? super.onCreate(savedInstanceState);

    ?????????????????? setContentView(R.layout.activity_main);

    ?

    ?????????????????? // 寫數據

    ?????????????????? // 私有文件

    ?????????????????? writeToLocal("private.txt", Context.MODE_PRIVATE);

    ?????????????????? // 可讀文件

    ?????????????????? writeToLocal("readable.txt", Context.MODE_WORLD_READABLE);

    ?????????????????? // 可寫文件

    ?????????????????? writeToLocal("writeable.txt", Context.MODE_WORLD_WRITEABLE);

    ?????????????????? // 可讀可寫文件

    ?????????????????? writeToLocal("readable_writeable.txt", Context.MODE_WORLD_READABLE

    ???????????????????????????????????? + Context.MODE_WORLD_WRITEABLE);

    ??????????????????

    ?????????????????? findViewById(R.id.btn_read_private).setOnClickListener(this);

    ?????????????????? findViewById(R.id.btn_write_private).setOnClickListener(this);

    ??????????????????

    ?????????????????? findViewById(R.id.btn_read_readable).setOnClickListener(this);

    ?????????????????? findViewById(R.id.btn_write_readable).setOnClickListener(this);

    ??????????????????

    ?????????????????? findViewById(R.id.btn_read_writeable).setOnClickListener(this);

    ?????????????????? findViewById(R.id.btn_write_writeable).setOnClickListener(this);

    ??????????????????

    ?????????????????? findViewById(R.id.btn_read_readable_writeable).setOnClickListener(this);

    ?????????????????? findViewById(R.id.btn_write_readable_writeable).setOnClickListener(this);

    ???????? }

    ?

    ???????? /**

    ???????? ?* 寫文件

    ???????? ?* @param fileName

    ???????? ?* @param mode

    ???????? ?*/

    ???????? private void writeToLocal(String fileName, int mode) {

    ?????????????????? try {

    ??????????????????????????? FileOutputStream fos = openFileOutput(fileName, mode);

    ??????????????????????????? fos.write(("第一個程序寫的數據:" + fileName).getBytes());

    ??????????????????????????? fos.flush();

    ??????????????????????????? fos.close();

    ?????????????????? } catch (Exception e) {

    ??????????????????????????? e.printStackTrace();

    ?????????????????? }

    ???????? }

    ????????

    ???????? /**

    ???????? ?* 哪一個控件被點擊, v對象就代表被點擊的對象

    ???????? ?*/

    ???????? @Override

    ???????? public void onClick(View v) {

    ?????????????????? switch (v.getId()) {

    ?????????????????? case R.id.btn_read_private:

    ??????????????????????????? readFile("private.txt");

    ??????????????????????????? break;

    ?????????????????? case R.id.btn_write_private:

    ??????????????????????????? writeFile("private.txt");

    ??????????????????????????? break;

    ?????????????????? case R.id.btn_read_readable:

    ??????????????????????????? readFile("readable.txt");

    ??????????????????????????? break;

    ?????????????????? case R.id.btn_write_readable:

    ??????????????????????????? writeFile("readable.txt");

    ??????????????????????????? break;

    ?????????????????? case R.id.btn_read_writeable:

    ??????????????????????????? readFile("writeable.txt");

    ??????????????????????????? break;

    ?????????????????? case R.id.btn_write_writeable:

    ??????????????????????????? writeFile("writeable.txt");

    ??????????????????????????? break;

    ?????????????????? case R.id.btn_read_readable_writeable:

    ??????????????????????????? readFile("readable_writeable.txt");

    ??????????????????????????? break;

    ?????????????????? case R.id.btn_write_readable_writeable:

    ??????????????????????????? writeFile("readable_writeable.txt");

    ??????????????????????????? break;

    ?????????????????? default:

    ??????????????????????????? break;

    ?????????????????? }

    ???????? }

    ?

    ???????? /**

    ???????? ?* 讀文件

    ???????? ?* @param fileName

    ???????? ?*/

    ???????? private void readFile(String fileName) {

    ?????????????????? try {

    ??????????????????????????? String path = basicPath + fileName;

    ???????????????????????????

    ??????????????????????????? BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));

    ??????????????????????????? String text = reader.readLine();

    ??????????????????????????? reader.close();

    ??????????????????????????? Toast.makeText(this, "讀取成功: " + text, 0).show();

    ?????????????????? } catch (Exception e) {

    ??????????????????????????? e.printStackTrace();

    ??????????????????????????? Toast.makeText(this, "讀取失敗: " + fileName, 0).show();

    ?????????????????? }

    ???????? }

    ????????

    ???????? /**

    ???????? ?* 寫文件

    ???????? ?* @param fileName

    ???????? ?*/

    ???????? private void writeFile(String fileName) {

    ?????????????????? try {

    ??????????????????????????? String path = basicPath + fileName;

    ???????????????????????????

    ??????????????????????????? FileOutputStream fos = new FileOutputStream(path);

    ???????????????????????????

    ??????????????????????????? fos.write("哈哈, 被我給黑了".getBytes());

    ???????????????????????????

    ??????????????????????????? fos.flush();

    ???????????????????????????

    ??????????????????????????? fos.close();

    ??????????????????????????? Toast.makeText(this, "寫入成功: " + fileName, 0).show();

    ?????????????????? } catch (Exception e) {

    ??????????????????????????? e.printStackTrace();

    ??????????????????????????? Toast.makeText(this, "寫入失敗: " + fileName, 0).show();

    ?????????????????? }

    ???????? }

    }

    ?

    

    總結

    以上是生活随笔為你收集整理的03_Android项目中读写文本文件的代码的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。