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. VMware在宿主上没有VMnet0、VMnet8,解决方法

    一开始,坐着上机实验,一直搞不通为什么虚拟机上的客户机可以ping通自己的ip也可以ping通自己本身的ip,但是主机ping不通虚拟机的客户机,也ping不通虚拟机的网关. 尝试了各种问题,也追出了 ...

  2. Hadoop — MapReduce原理解析

    1. 概述 Mapreduce是一个分布式运算程序的编程框架,是用户开发"基于hadoop的数据分析应用"的核心框架: Mapreduce核心功能是将用户编写的业务逻辑代码和自带默 ...

  3. 转:rabbitmq——用户管理

    原文:http://my.oschina.net/hncscwc/blog/262246?p={{currentPage-1}} 安装最新版本的rabbitmq(3.3.1),并启用managemen ...

  4. geotrellis使用(四十)优雅的处理请求超过最大层级数据

    前言 要说清楚这个题目对我来说可能都不是一件简单的事情,我简单尝试. 研究 GIS 的人应该都清楚在 GIS 中最常用的技术是瓦片技术,无论是传统的栅格瓦片还是比较新颖的矢量瓦片,一旦将数据切好瓦片就 ...

  5. MongoDB 删除文档

    ongoDB remove()函数是用来移除集合中的数据. MongoDB数据更新可以使用update()函数.在执行remove()函数前先执行find()命令来判断执行的条件是否正确,这是一个比较 ...

  6. 介绍Docker仓库

    仓库(Repository)是集中存放镜像的地方. 一个容易混淆的概念是注册服务器(Registry).实际上注册服务器是管理仓库的具体服务器,每个服务器上可以有多个仓库,而每个仓库下面有多个镜像.从 ...

  7. Spring中配置DataSource的六种方式

    第一种:beans.xml <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource ...

  8. Launcher3 HotSeat显示名称

    今天闲的无聊,研究了下launcher代码,看到Hotseat.java的时候,想起来以前有做过显示hotseat中应用名称,因为换了公司代码都没拿出来,就想在试着修改,看了很久发现无从下手,记得ho ...

  9. 干货!Android Studio快捷键VS Eclipse快捷键~亲测!

    eclipse as 英文描述 中文描述 ctrl+shift+r ctrl+shift+r Navigate->File 找工作空间的文件 ctrl+shift+t ctrl+shift+t ...

  10. 2017年校园招聘ios面试题

    一.搜狐快站 1.谈谈你做过的项目: 2.项目中最有成就感的部分: 3.倒计时如何实现?(NSTimer,还有其他的实现方式吗): 4.UIButton的继承关系? 5.iOS中可以进行输入的控件?( ...