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项目中读写文本文件的代码的更多相关文章
- VS Code项目中共享自定义的代码片段方案
VS Code项目中共享自定义的代码片段方案 一.问题背景 项目中注释风格不统一,如何统一注释风格 一些第三方组件库名称太长,每次使用都需要找文档,然后复制粘贴 部分组件库有自己的Snippets插件 ...
- Vue2/3 项目中的 ESLint + Prettier 代码检测格式化风格指南
Vue2/3 项目中的 ESLint + Prettier 代码检测格式化风格指南 因为平时都是使用 VSCode ESLint + Prettier 检测格式化不规范代码,但是随着接手的项目越来越多 ...
- unity3D项目中如何避免硬代码(C#)
平时做项目,代码中是不允许出现硬代码的,一般我们是怎么处理的呢? 那么硬代码又是什么呢? 我们俗称的硬代码:eg: label.text = "欢迎来到梦幻岛"; 这样我们俗 ...
- 在PHP项目中使用Standford Moss代码查重系统
Standford Moss 系统是斯坦福大学大名鼎鼎的代码查重系统,它可以查出哪些同学提交的代码是抄袭别人的,从而将提交结果拒之门外.它对一切希望使用该系统的人都是开放的,那么在PHP的项目中如何使 ...
- Findbug在项目中的运用--提高代码质量
FindBugs是一个静态分析工具,它检查类或者 JAR文件,将字节码与一组缺陷模式进行对比以发现可能的问题.有了静态分析工具,就可以在不实际运行程序的情况对软件进行分析 第一 手动安装 在Ec ...
- 项目中解决实际问题的代码片段-javascript方法,Vue方法(长期更新)
总结项目用到的一些处理方法,用来解决数据处理的一些实际问题,所有方法都可以放在一个公共工具方法里面,实现不限ES5,ES6还有些Vue处理的方法. 都是项目中来的,有代码跟图片展示,长期更新. 1.获 ...
- 使用 Lombok 简化项目中无谓的Java代码
在写使用Java时,难免会有一些模板代码要写,不然get/set,toString, hashCode, close 资源,定义构造函数等等.代码会显得很冗余,很长.Lombok项目可以是我们摆脱这些 ...
- Maven 项目中使用mybatis-generator生成代码
在使用Maven构建SSM项目时,使用mybatis-generator插件自动生成代码 一.目录结构 bean:用来存放生成的实体类 dao:用来存放生成的 *mapper.java文件 mappe ...
- 项目中使用的ajax代码_:觉得还好
POST>> submitHandler:function(form){ var username = $('#user_name').val(); var password = $('# ...
随机推荐
- if-case-循环语句
IF语句 drop procedure if exists p_hello_world; create procedure p_hello_world(in v_id int) begin ) the ...
- 存出和载入Docker镜像
存出镜像 如果要导出镜像到本地文件,可以使用 docker save 命令. $ sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL ...
- 实验与作业(Python)-文件操作
1.CSV文件的处理 下载-身份证号文件 导入: 读入"身份证号.txt",然后打印出来.注意:是否多打了一行,为什么? 读入"身份证号.txt",然后存储到& ...
- Jupyter Notebook 添加目录
1. 安装 jupyter_contrib_nbextensions pip install jupyter_contrib_nbextensions 2. 配置 nbextension jupyt ...
- 使用kprobes查看内核内部信息
前言:使用printk打印变量等方法,是调试内核的有效方法之一,但是这种方法必须重新构建并用新内核启动,调试效率比较低.以内核模块的方式使用kprobes.jprobes,就可以在任意地址插入侦测器, ...
- Core Python Programming一书中关于深浅拷贝的错误
该书关于深浅拷贝的论述: 6.20. *Copying Python Objects and Shallow and Deep Copies "when shallow copies are ...
- C++语言编译系统提供的内部数据类型的自动隐式转换
C++语言编译系统提供的内部数据类型的自动隐式转换规则如下: 程序在执行算术运算时,低类型自动隐式转换为高类型. 在函数调用时,将实参值赋给形参,系统隐式的将实参转换为形参的类型,并赋值给形参. 函数 ...
- Dynamics CRM The difference between UserId and InitiatingUserId in Plugin
对于这两者的不同,MSDN的解释如下 • IExecutionContext.UserId Property: Gets the global unique identifier of the sys ...
- springMVC源码分析--HandlerInterceptor拦截器(一)
对SpringMVC有所了解的人肯定接触过HandlerInterceptor拦截器,HandlerInterceptor接口给我们提供了3个方法: (1)preHandle: 在执行controll ...
- ProgressBar的简单使用
当我们的应用在进行耗时操作时,显示一个进度条呈现给用户,让用户知道当前进度是一个很好的体验,接下来我们就来简单了解下ProgressBar(本文主要针对初学者,大神可以绕开啦),先看效果图: 进度条P ...