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. web缓存之--http缓存机制

    一.web缓存可以分为数据库缓存.代理服务器缓存.浏览器缓存. 其中浏览器缓存又包含很多内容:http缓存.indexDb.cookie.localStorage等.本片只讨论http缓存相关内容. ...

  2. python中的函数(定义、多个返回值、默认参数、参数组)

    函数定义 在python中函数的定义以及调用如下代码所示: def test(x): y = x+1 return y result = test(2) print(result) 多个返回值的情况 ...

  3. C#系统之垃圾回收

    1. using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...

  4. 剑指架构师系列-Logstash分布式系统的日志监控

    Logstash主要做由三部署组成: Collect:数据输入 Enrich:数据加工,如过滤,改写等 Transport:数据输出 下面来安装一下: wget https://download.el ...

  5. Mac下安装PEAR

    The following instructions install PEAR and PECL on Mac OS X under/usr/local/. PECL is bundled with ...

  6. 线程停止与volatile

    1.使用标志位停止线程 在Java中希望停止线程,可以使用设置标志位的方法,如下例所示: class SimpleTask implements Runnable{ private boolean s ...

  7. NLP系列(2)_用朴素贝叶斯进行文本分类(上)

    作者:龙心尘 && 寒小阳 时间:2016年1月. 出处: http://blog.csdn.net/longxinchen_ml/article/details/50597149 h ...

  8. Dubbo框架应用之(二)--服务治理

    Dubbo服务治理全貌图 当我们现有ITOO平台系统的业务随着用户的逐渐增大,设计的业务越来越广,系统会异常的复杂,在大规模服务之前,我们可以采用的是RMI或Hessian等工具,暴露和引用远程服务, ...

  9. Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/43536355 大家好,在上一篇文章当中,我们学习了Android属性动画的基本用法 ...

  10. Swift基础之侧滑Cell显示操作按钮

    好久没写过博客了,今天展示一个UITableView基础的内容侧滑Cell的方法使用,之前写过OC语言的http://blog.csdn.net/hbblzjy/article/details/517 ...