1. 编写一下Android界面的项目

  1. 使用默认的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>

  1. 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项目中读写文本文件的代码相关推荐

  1. android开发使用c+_如何在Android项目中开始使用C ++代码

    android开发使用c+ by Onur Tuna 通过Onur Tuna 如何在Android项目中开始使用C ++代码 (How to start using C++ code in your ...

  2. [css] 如何清除在项目中无用的css代码呢?

    [css] 如何清除在项目中无用的css代码呢? 1.IDE中,会对没有使用到的样式,自己进行检测,删除时候,还需要手动删除. 2.webpack中,有基于消除无用css的插件(purifycss-w ...

  3. Python之深入解析Vulture如何一键找出项目中所有无效的代码

    一.前言 Vulture 可以在Python程序中查找未使用的代码,这对于清理和查找大型项目(代码库)中的错误非常有用. 不过由于 Python 的动态特性,像 Vulture 这样的静态代码分析器很 ...

  4. 如何在Visual Studio项目中正确添加汇编代码 .

    引用注明>> [作者:张佩][镜像:www.yiiyee.cn/blog] 1.      问题描述 在以往的编程经历中,本人最常使用的汇编代码是__asm {int 3}.它可以在我的代 ...

  5. 项目中使用 husky 格式化代码和校验 commit 信息

    大家好,我是前端西瓜哥.今天我们学习使用 husky 工具,在 commit 的时候做一些风格的校验工作,包括 commit 信息格式化和文件格式化. git hook 和 husky git hoo ...

  6. 项目中使用ecTable的代码

    这是不配置extremetable.properties的初始样式. 这是配置了extremetable.properties自定义的样式,主要是加了一个页数下拉框,可以选择第几页,每页几行.这个下拉 ...

  7. Vue 项目中高亮格式化 xml 代码

    效果演示 安装插件 $ npm install highlight.js --save 代码实现 xml原文如下: <?xml version=\"1.0\" encodin ...

  8. pandas项目中使用的一些代码总结

    在使用逻辑筛选的时候需要注意: 逻辑符号|和or虽然都可以用,但是并不完全等同,or只要左边的成立了,右边就不会执行了,|只要左右有一个成立,都行,例子:df[(df 表达式1) | (df 表达式2 ...

  9. python pycharm如何全局(整个项目中)搜索指定代码?(CTRL+SHIFT+F)全局字符串搜索

    CTRL+SHIFT+F

最新文章

  1. vue 为全局变量赋值_vue设置全局变量和修改
  2. 18行代码AC_Wet Shark and Bishops CodeForces - 621B(数学推导+映射)
  3. 网站搭建从零开始(七) WordPress站点的完善
  4. Redis学习与实战之字符串命令
  5. 硬盘序列号示例_序列化代理模式示例
  6. android线性布局快捷键,【整理】Android图形界面知识学习与总结之:Linear Layout线性布局...
  7. 下载加载linux下用vmware-mount挂载vmdk虚拟硬盘分区
  8. 吃是为了肉体,喝是为了灵魂
  9. Failed to create AppDomain 'xxx'. Exception has been Failed to create AppDomain
  10. 树莓派环境处理_树莓派安装raspbian并配置开发环境
  11. 行星月球科学探索成绩斐然 桌面实验或可理解黑洞性质
  12. 基于 SpringBoot 手写 RPC 框架
  13. 移动硬盘位置不可用无法访问函数不正确修复方法?
  14. 如何正确控制springboot中bean的加载顺序总结
  15. php lumen 框架优点,微框架Lumen 特性
  16. 黑苹果gtx1050显卡驱动问题
  17. 盛世昊通愿天下无拐,期待所有失孤家庭早日团圆
  18. 三角函数反函数c语言,高中数学反函数有哪些 反三角函数的所有公式
  19. python模拟的王者游戏
  20. 三阶段 软件安装和环境配置

热门文章

  1. Python-流程控制
  2. VTK:vtkCompassWidget用法实战
  3. OpenCASCADE绘制测试线束:数据交换命令之IGES 命令
  4. wxWidgets:wxHashSet类用法
  5. boost::lambda::switch_statement用法的测试程序
  6. boost::shared_ptr用法测试程序
  7. GDCM:gdcm::EquipmentManufacturer的测试程序
  8. GDCM:无效的DICOM文件的测试程序
  9. 使用PORT对HOSTNAME执行DICOM Q / R操作的测试程序
  10. Boost:传输文件的测试程序