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. VS Code项目中共享自定义的代码片段方案

    VS Code项目中共享自定义的代码片段方案 一.问题背景 项目中注释风格不统一,如何统一注释风格 一些第三方组件库名称太长,每次使用都需要找文档,然后复制粘贴 部分组件库有自己的Snippets插件 ...

  2. Vue2/3 项目中的 ESLint + Prettier 代码检测格式化风格指南

    Vue2/3 项目中的 ESLint + Prettier 代码检测格式化风格指南 因为平时都是使用 VSCode ESLint + Prettier 检测格式化不规范代码,但是随着接手的项目越来越多 ...

  3. unity3D项目中如何避免硬代码(C#)

    平时做项目,代码中是不允许出现硬代码的,一般我们是怎么处理的呢? 那么硬代码又是什么呢? 我们俗称的硬代码:eg:   label.text = "欢迎来到梦幻岛";  这样我们俗 ...

  4. 在PHP项目中使用Standford Moss代码查重系统

    Standford Moss 系统是斯坦福大学大名鼎鼎的代码查重系统,它可以查出哪些同学提交的代码是抄袭别人的,从而将提交结果拒之门外.它对一切希望使用该系统的人都是开放的,那么在PHP的项目中如何使 ...

  5. Findbug在项目中的运用--提高代码质量

     FindBugs是一个静态分析工具,它检查类或者 JAR文件,将字节码与一组缺陷模式进行对比以发现可能的问题.有了静态分析工具,就可以在不实际运行程序的情况对软件进行分析 第一 手动安装 在Ec ...

  6. 项目中解决实际问题的代码片段-javascript方法,Vue方法(长期更新)

    总结项目用到的一些处理方法,用来解决数据处理的一些实际问题,所有方法都可以放在一个公共工具方法里面,实现不限ES5,ES6还有些Vue处理的方法. 都是项目中来的,有代码跟图片展示,长期更新. 1.获 ...

  7. 使用 Lombok 简化项目中无谓的Java代码

    在写使用Java时,难免会有一些模板代码要写,不然get/set,toString, hashCode, close 资源,定义构造函数等等.代码会显得很冗余,很长.Lombok项目可以是我们摆脱这些 ...

  8. Maven 项目中使用mybatis-generator生成代码

    在使用Maven构建SSM项目时,使用mybatis-generator插件自动生成代码 一.目录结构 bean:用来存放生成的实体类 dao:用来存放生成的 *mapper.java文件 mappe ...

  9. 项目中使用的ajax代码_:觉得还好

    POST>> submitHandler:function(form){ var username = $('#user_name').val(); var password = $('# ...

随机推荐

  1. --save-dev 和 --save的区别

    1. 我们在使用npm install xx --save-dev / --save安装模块或插件的时候,会将他们写入到 package.json 文件,那到底有什么区别呢? --save-dev:会 ...

  2. Unity CommandBuffer的一些学习整理

    1.前言 近期在整理CommandBuffer这块资料,之前的了解一直较为混乱. 算不上新东西了,但个人觉得有些时候要比加一个摄像机再转RT廉价一些,至少省了深度排序这些操作. 本文使用两个例子讲解C ...

  3. C++笔记007:易犯错误模型——类中为什么需要成员函数

    先看源码,在VS2010环境下无法编译通过,在VS2013环境下可以编译通过,并且可以运行,只是运行结果并不是我们期待的结果. 最初通过MyCircle类定义对象c1时,为对象分配内存空间,r没有初始 ...

  4. ES6(Decorator(修饰器))

    Decorator(修饰器) 1.基本概念 函数用来修改 类 的行为 1.Decorator 是一个函数 2.通过Decorator(修饰器)能修改 类 的行为(扩展 类 的功能)3.Decorato ...

  5. [ExtJS5学习笔记]第三十六节 报表组件mzPivotGrid

    mzPivotGrid 是一个报表组件,采用这个组件之后,可以令你的应用体现更多的价值. 什么是pivot grid 什么是mzPivotGrid 学习资源 与图表组件的融合 什么是pivot gri ...

  6. 详解EBS接口开发之供应商导入

    (一)供应商常用标准表简介 1.1   常用标准表 如下表中列出了与供应商相关的表和说明: 表名 说明 其他信息 ap_suppliers 供应商头表 供应商的头信息如:供应商名.供应商编码.税号等 ...

  7. PhysicsJoint

    1 PhysicsJoint的使用 T09Join.h #ifndef__T09Joint_H__ #define__T09Joint_H__ #include"T32.h" cl ...

  8. 自制Linux重命名命令

    相比于Windows上的ren命名,Linux还真的是没有一个特定的重命名的命令.(虽然可以间接的使用mv来实现).下面我就来自己写一个简单的重命名命令. 准备工作 操作系统: Linux内核的系统都 ...

  9. Android加速度传感器

    Android加速度传感器 效果图 手机平放桌面的两张截屏,数据一直在刷新 源码 下载地址(Android Studio工程):http://download.csdn.net/detail/q487 ...

  10. UNIX环境高级编程——创建孤儿进程

    /* 创建孤儿进程 父进程终止后,向子进程发送挂断信号,又接着发送继续信号. */ #include <stdio.h> #include <stdlib.h> #includ ...