1、当前是把文件保存当前手机的app的data目录下

我们来看看操作保存文件的业务类

package contract.test.savafileapplication;

import android.content.Context;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; public class FileService {
private Context context; public FileService(Context context) {
this.context = context;
} public void saveFile(String fileName, String fileContext) throws Exception {
FileOutputStream fileOutputStream = context.openFileOutput(fileName,Context.MODE_WORLD_READABLE);
fileOutputStream.write(fileContext.getBytes());
fileOutputStream.close();
} public String readFile(String filename) throws IOException { FileInputStream fileInputStream = context.openFileInput(filename); byte[] bytes = new byte[1024];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
/*byte是基本数据类型 如int类型
Byte是byte的包装类*/
int len = 0;
while ((len = fileInputStream.read(bytes))!=-1)
{ byteArrayOutputStream.write(bytes,0,len);
}
byteArrayOutputStream.close();
fileInputStream.close();
String str = new String(byteArrayOutputStream.toString());
return str;
}
}
openFileInput保存文件有四种类型:

openFileOutput(“a.txt”,Context.MODE_WORLD_READABLE)方法第一个参数是文件名,若不存在这个文件,则自动创建,第二个参数是操作模式共四种:

Context.MODE_PRIVATE    =  0
Context.MODE_APPEND    =  32768
Context.MODE_WORLD_READABLE =  1
Context.MODE_WORLD_WRITEABLE =  2

Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。
如果希望文件被其他应用读和写,可以传入:
openFileOutput(“itcast.txt”, Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

activity的代码是:

package contract.test.savafileapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import java.io.IOException; public class MyActivity extends Activity {
private Button save , show;
private EditText filename, filecontext;
private TextView showContext; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
save = (Button)this.findViewById(R.id.saveButton);
show = (Button)this.findViewById(R.id.showButton);
filename = (EditText)this.findViewById(R.id.fileName_ET);
filecontext = (EditText)this.findViewById(R.id.saveContext_ET); showContext = (TextView)this.findViewById(R.id.showConTextView); save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = filename.getText().toString();
String context = filecontext.getText().toString();
FileService fileService = new FileService(getApplicationContext());
try {
fileService.saveFile(name,context);
Toast.makeText(getApplicationContext(),"文件保存成功!",Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"文件保存失败!",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FileService fileService = new FileService(getApplicationContext());
String name = filename.getText().toString();
try {
String str = fileService.readFile(name);
Toast.makeText(getApplicationContext(),"文件读取成功!",Toast.LENGTH_LONG).show();
showContext.setText(str);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"文件读取失败!",Toast.LENGTH_LONG).show();
}
}
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入要保存的文件名:"
/>
<EditText
android:id="@+id/fileName_ET"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:hint="请输入要保存的文件名!"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请您输入要保存的内容:"
/>
<EditText
android:id="@+id/saveContext_ET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请您在此处输入文件内容!"
/>
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
/>
<Button
android:id="@+id/showButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show"
/>
<TextView
android:id="@+id/showConTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
 

黎活明8天快速掌握android视频教程--12_文件的保存与读取的更多相关文章

  1. 黎活明8天快速掌握android视频教程--16_采用SharedPreferences保存用户偏好设置参数

    SharedPreferences保存的数据是xml格式,也是存在数据保存的下面四种权限: 我们来看看 我们来看看具体的业务操作类: /** * 文件名:SharedPrecences.java * ...

  2. 黎活明8天快速掌握android视频教程--14_把文件存放在SDCard

    把文件保存在手机的内部存储空间中 1 首先必须在清单文件中添加权限 <?xml version="1.0" encoding="utf-8"?> & ...

  3. 黎活明8天快速掌握android视频教程--22_访问通信录中的联系人和添加联系人

    Android系统中联系人的通讯录的contentProvide是一个单独的apk,显示在界面的contact也是一个独立的apk,联系人apk通过contentProvide访问底层的数据库. 现在 ...

  4. 黎活明8天快速掌握android视频教程--25_网络通信之资讯客户端

    1 该项目的主要功能是:后台通过xml或者json格式返回后台的视频资讯,然后Android客户端界面显示出来 首先后台新建立一个java web后台 采用mvc的框架 所以的servlet都放在se ...

  5. 黎活明8天快速掌握android视频教程--24_网络通信之网页源码查看器

    1 该项目的主要功能就是从将后台的html网页在Android的界面上显示出来 后台就是建立一个java web工程在工程尚建立一个html或者jsp文件就可以了,这里主要看Android客户端的程序 ...

  6. 黎活明8天快速掌握android视频教程--23_网络通信之网络图片查看器

    1.首先新建立一个java web项目的工程.使用的是myeclipe开发软件 图片的下载路径是http://192.168.1.103:8080/lihuoming_23/3.png 当前手机和电脑 ...

  7. 黎活明8天快速掌握android视频教程--21_监听ContentProvider中数据的变化

    采用ContentProvider除了可以让其他应用访问当前的app的数据之外,还有可以实现当app的数据发送变化的时候,通知注册了数据变化通知的调用者 其他所有的代码都和第20讲的一样,不同的地方看 ...

  8. 黎活明8天快速掌握android视频教程--20_采用ContentProvider对外共享数据

    1.内容提供者是让当前的app的数据可以让其他应用访问,其他应该可以通过内容提供者访问当前app的数据库 contentProvider的主要目的是提供一个开发的接口,让其他的应该能够访问当前应用的数 ...

  9. 黎活明8天快速掌握android视频教程--19_采用ListView实现数据列表显示

    1.首先整个程序也是采用mvc的框架 DbOpenHelper 类 package dB; import android.content.Context; import android.databas ...

随机推荐

  1. Linux显示行号设置

    linux显示行号设置 第一步,打开vim vi ~/.vimrc 第二步,在该文件中加入一行,命令如下: set nu # 显示行号 set nonu # 不显示行号 微信公众号:喵哥解说 公众号介 ...

  2. 【asp.net core 系列】 1 带你了解一下asp.net core

    0. 前言 这是一个新的系列,名字是<ASP.NET Core 入门到实战>.这个系列主讲ASP.NET Core MVC,辅助一些前端的基础知识(能用来实现我们需要的即可,并非主讲).同 ...

  3. 从汉堡加料说起——浅谈C#中的Decorator模式

    相信大家都在都在汉堡店吃过汉堡,有些汉堡店很有特色,推出了汉堡订制服务,即,可以在汉堡中加料,加肉饼,加生菜之类(有点类似我们本地的肥肠粉里面加冒结子).更是让不少吃货大快朵颐,大呼过瘾,加6,7层肉 ...

  4. 判断IP地址的合法性

    每台计算机都有独一无二的编号,称为ip地址,每个合法的ip地址由‘.’分隔开的4个数字组成,每个数字的取值范围为0--255 输入一个字符串,判断其是否为合法的IP地址,若是输出‘YES’,否则输出‘ ...

  5. Java实现 LeetCode 811 子域名访问计数 (暴力)

    811. 子域名访问计数 一个网站域名,如"discuss.leetcode.com",包含了多个子域名.作为顶级域名,常用的有"com",下一级则有" ...

  6. Java实现 蓝桥杯 算法训练 出现次数最多的整数

    算法训练 出现次数最多的整数 时间限制:1.0s 内存限制:512.0MB 提交此题 问题描述 编写一个程序,读入一组整数,这组整数是按照从小到大的顺序排列的,它们的个数N也是由用户输入的,最多不会 ...

  7. Java实现 LeetCode 349 两个数组的交集

    349. 两个数组的交集 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2] 示例 2: 输入: num ...

  8. Java实现 稀疏矩阵乘积

    稀疏矩阵乘积 描述 给定两个N × N的稀疏矩阵A和B,其中矩阵A有P个元素非0,矩阵B有Q个元素非0.请计算两个矩阵的乘积C = A × B并且输出C中所有非0的元素. 输入 第一行包含三个整数N, ...

  9. java实现第四届蓝桥杯三部排序

    三部排序 题目描述 一般的排序有许多经典算法,如快速排序.希尔排序等. 但实际应用时,经常会或多或少有一些特殊的要求.我们没必要套用那些经典算法,可以根据实际情况建立更好的解法. 比如,对一个整型数组 ...

  10. iOS-NSString常见方法

    </pre><pre name="code" class="html">#import <Foundation/Foundatio ...