黎活明8天快速掌握android视频教程--12_文件的保存与读取
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_文件的保存与读取的更多相关文章
- 黎活明8天快速掌握android视频教程--16_采用SharedPreferences保存用户偏好设置参数
SharedPreferences保存的数据是xml格式,也是存在数据保存的下面四种权限: 我们来看看 我们来看看具体的业务操作类: /** * 文件名:SharedPrecences.java * ...
- 黎活明8天快速掌握android视频教程--14_把文件存放在SDCard
把文件保存在手机的内部存储空间中 1 首先必须在清单文件中添加权限 <?xml version="1.0" encoding="utf-8"?> & ...
- 黎活明8天快速掌握android视频教程--22_访问通信录中的联系人和添加联系人
Android系统中联系人的通讯录的contentProvide是一个单独的apk,显示在界面的contact也是一个独立的apk,联系人apk通过contentProvide访问底层的数据库. 现在 ...
- 黎活明8天快速掌握android视频教程--25_网络通信之资讯客户端
1 该项目的主要功能是:后台通过xml或者json格式返回后台的视频资讯,然后Android客户端界面显示出来 首先后台新建立一个java web后台 采用mvc的框架 所以的servlet都放在se ...
- 黎活明8天快速掌握android视频教程--24_网络通信之网页源码查看器
1 该项目的主要功能就是从将后台的html网页在Android的界面上显示出来 后台就是建立一个java web工程在工程尚建立一个html或者jsp文件就可以了,这里主要看Android客户端的程序 ...
- 黎活明8天快速掌握android视频教程--23_网络通信之网络图片查看器
1.首先新建立一个java web项目的工程.使用的是myeclipe开发软件 图片的下载路径是http://192.168.1.103:8080/lihuoming_23/3.png 当前手机和电脑 ...
- 黎活明8天快速掌握android视频教程--21_监听ContentProvider中数据的变化
采用ContentProvider除了可以让其他应用访问当前的app的数据之外,还有可以实现当app的数据发送变化的时候,通知注册了数据变化通知的调用者 其他所有的代码都和第20讲的一样,不同的地方看 ...
- 黎活明8天快速掌握android视频教程--20_采用ContentProvider对外共享数据
1.内容提供者是让当前的app的数据可以让其他应用访问,其他应该可以通过内容提供者访问当前app的数据库 contentProvider的主要目的是提供一个开发的接口,让其他的应该能够访问当前应用的数 ...
- 黎活明8天快速掌握android视频教程--19_采用ListView实现数据列表显示
1.首先整个程序也是采用mvc的框架 DbOpenHelper 类 package dB; import android.content.Context; import android.databas ...
随机推荐
- [256个管理学理论]006.刺猬效应(Hedgehog Effect)
刺猬效应(Hedgehog Effect) 来自于大洋彼岸的让你看不懂的解释: 刺猬效应(刺猬法则)就是人际交往中的“心理距离效应”.人与人之间都应该保持这条底线,过犹不及. 刺猬效应强调的就是人际交 ...
- Linux(一):VMware安装出现的问题
目录 1 兼容性问题 2 VMware打卡虚拟机提示"此虚拟机可能已被复制或移动" 1 兼容性问题 问题:VMware Workstation 与 Device/Credentia ...
- 使用fileupload组件
1. 进行文件上传时, 表单需要做的准备: 1). 请求方式为 POST: <form action="uploadServlet" method="post&qu ...
- Javascript函数闭包详解(通俗易懂
许多书上闭包过于复杂讲解难懂,自己理解了一下并总结啦~ 讲闭包之前,需要先明白以下几个概念. 总之,函数执行时所在的作用域,是定义时的作用域,而不是调用时所在的作用域. 1.执行上下文(executi ...
- JVM系列.JDK演进历史
Java程序员为什么要了解虚拟机相关的知识 Java程序在设计之初就号称"一次编译,到处运行".Java之所以能做到"一次编译,处处运行"是因为Java虚拟机隐 ...
- Rocket - interrupts - Xbar
https://mp.weixin.qq.com/s/icPGf4KdSOudwuNpLxdo7w 简单介绍Xbar的实现. 1. 简单介绍 IntXbar主要用于把上游多个中断源的中断组合在一起,然 ...
- Rocket - config - Parameters
https://mp.weixin.qq.com/s/uLEr9gAFaMDIXa8S9xJVTw 介绍配置类Parameters及其伴生对象的实现. 参考链接: https://docs.q ...
- 本地计算机上的MySQL80服务启动后停止,某些服务在未由其他服务或者程序使用时将自动停止
是由于mysql server XX 路径下的my.ini文件发生错误. 高版本的mysql server的my.ini文件不在mysql server XX路径下,在programdata文件夹(查 ...
- 企业级Python开发大佬利用网络爬虫技术实现自动发送天气预告邮件
前天小编带大家利用Python网络爬虫采集了天气网的实时信息,今天小编带大家更进一步,将采集到的天气信息直接发送到邮箱,带大家一起嗨~~拓展来说,这个功能放在企业级角度来看,只要我们拥有客户的邮箱,之 ...
- Java实现 LeetCode 466 统计重复个数
466. 统计重复个数 定义由 n 个连接的字符串 s 组成字符串 S,即 S = [s,n].例如,["abc", 3]="abcabcabc". 另一方面, ...