03_Android项目中读写文本文件的代码

  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();

                   }

         }

}

 



时间: 2024-07-31 14:19:41

03_Android项目中读写文本文件的代码的相关文章

急需解答如何在一个项目中如何将一个html代码再另一个项目中转化为jsp代码

问题描述 急需解答如何在一个项目中如何将一个html代码再另一个项目中转化为jsp代码 如何将html的代码转化为jsp代码,求解答!如何变成内容都是登录页面 解决方案 黑马程序员--一个划拳的小项目代码 解决方案二: 先把hmtl页面复制过去,再把 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path =

PHP中读写文件实现代码_php技巧

在PHP中读写文件,可以用到一下内置函数: 1.fopen(创建文件和打开文件) 语法: 复制代码 代码如下: fopen(filename,mode) filename,规定要打开的文件.mode,打开文件的模式,可能的值见下表. mode 说明 "r" 只读方式打开,将文件指针指向文件开头. "r+" 读写方式打开,将文件指针指向文件开头. "w" 写入方式打开,将文件指针指向文件开头并将文件大小截为零.如果文件不存在则尝试创建. "

在项目中使用不安全代码【转】

from:http://bingya.javaeye.com/blog/657978 Unsafe code may only appear if compiling with /unsafe 文章分类:.net编程 要在vs.net中使用unsafe code 我们必须在项目的属性中设置一下,设置方法如下: 点项目属性(Properties)->生成(Build)->常规(General)中:钩上允许不安全代码(Allow unsafe code)     C#代码  public stat

asp.net中读写文件实现代码

写入分为续写和覆盖 只需改变第一个参数的值就可切换 代码如下:     代码如下 复制代码 /// <summary>         /// 内容写入到文本文件         /// </summary>         /// <param name="count">状态,判断是续写还是覆盖</param>         /// <param name="fileName">文件名称</par

struts项目中导出word excel不成功 麻烦大虾看一下

问题描述 <%@pagelanguage="java"pageEncoding="gbk"%><%@tagliburi="http://struts.apache.org/tags-bean"prefix="bean"%><%@tagliburi="http://struts.apache.org/tags-html"prefix="html"%>&l

ASP.NET中彩票项目中的计算复式投注的注数的方法

从别人做的项目中抽取出的代码:  

用于统计项目中代码总行数的Python脚本分享

  这篇文章主要介绍了用于统计项目中代码总行数的Python脚本分享,本文直接给出实现代码,需要的朋友可以参考下 最近需要统计一下项目中代码的总行数,写了一个Python小程序,不得不说Python是多么的简洁,如果用Java写至少是现在代码的2倍. [code] import os path="/Users/rony/workspace/ecommerce/ecommerce/hot-deploy/" global totalcount totalcount =0 def cfile

编写高质量代码改善java程序的151个建议——[110-117]异常及Web项目中异常处理

何为异常处理? 异常处理,英文名为exceptional handling, 是代替日渐衰落的error code方法的新法,提供error code 所未能具体的优势.异常处理分离了接收和处理错误代码.这个功能理清了编程者的思绪,也帮助代码增强了可读性,方便了维护者的阅读和理解. java语言中,异常处理可以确保程序的健壮性,提高系统的可用率.但是java api 提供的异常都是比较低级的,所以有了'提倡异常封装'                                        

【IOS-COCOS2D-X 游戏开发之七】整合COCOS2DX的ANDROID项目到XCODE项目中,XCODE编写&amp;编译代码,ANDROID导入打包运行即可!

本站文章均为 李华明Himi 原创,转载务必在明显处注明:  转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/android-game/667.html 此篇针对较早的-x引擎讲解的,最新的可以参考: [Cocos2d-X(2.x) 游戏开发系列之二]cocos2dx最新2.0.1版本跨平台整合NDK+Xcode,Xcode编写&编译代码,Android导入打包运行即可!   之前有两节介绍了mac下配置Android NDK并搭建Cocos2dX以