Android-openFileInput openFileOutput
Android设计了一套可以操作自身APP目录文件对API openFileInput openFileOutput,读取只需传入文件名,写入需要传入文件名 与 权限模式
界面:
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <EditText
android:id="@+id/et_output"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请写入数据到文件"
/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示读取处理到信息:"
/> <TextView
android:id="@+id/tv_input"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/black" android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
/> </LinearLayout> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"> <Button
android:id="@+id/bt_output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入"
/> <Button
android:id="@+id/bt_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取"
android:layout_alignParentRight="true"
/> </RelativeLayout> </LinearLayout>
openFileInput openFileOutpu 读写操作代码:
package liudeli.datastorage; import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; public class MainActivity3 extends Activity implements View.OnClickListener { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3); initViewListener(); getFilesDir(); // 得到当前APP应用files目录
getCacheDir();// 得到当前APP应用cache缓存目录
} private EditText etOutpu;
private TextView tvInput;
public Button btOutput, btInput; private void initViewListener() {
etOutpu = findViewById(R.id.et_output);
tvInput = findViewById(R.id.tv_input); btOutput = findViewById(R.id.bt_output);
btInput = findViewById(R.id.bt_input); btOutput.setOnClickListener(this);
btInput.setOnClickListener(this); // 让TextView获得焦点,TextView就可以滚动了
tvInput.setSelected(true);
} @Override
protected void onDestroy() {
super.onDestroy();
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_input: {
// Android提供了操作自身APP文件目录的API openFileInput
try {
File file = new File(getFilesDir().getAbsolutePath() ,"my_open_file_file.txt");
if (!file.exists()) {
Toast.makeText(MainActivity3.this, "文件不存在", Toast.LENGTH_LONG).show();
return;
} FileInputStream fileInputStream = openFileInput("my_open_file_file.txt");
byte[] bytes = new byte[1024];
int len; while (-1 != (len = fileInputStream.read(bytes))) {
StringBuffer sb = new StringBuffer();
sb.append(new String(bytes, 0 ,len));
tvInput.setText("" + sb.toString());
}
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
break;
}
case R.id.bt_output: {
String outputStr = etOutpu.getText().toString();
if (TextUtils.isEmpty(outputStr)) {
Toast.makeText(MainActivity3.this, "请输入内容!", Toast.LENGTH_SHORT).show();
return;
} // Android提供了操作自身APP文件目录的API openFileOutput
try { // MODE_APPEND 私有模式,每次会进行覆盖,是比较安全的模式
// MODE_WORLD_READABLE 全局可读模式
// MODE_WORLD_WRITEABLE 全局可写模式
// MODE_WORLD_READABLE|MODE_WORLD_WRITEABLE 全局可读可写模式 FileOutputStream fos = openFileOutput("my_open_file_file.txt", MODE_PRIVATE);
fos.write(outputStr.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
break;
}
default:
break;
}
}
}
操作的文件目录:使用 openFileInput openFileOutput 对文件进行操作,系统会自动在当前APP目录创建files文件夹/文件
Android-openFileInput openFileOutput的更多相关文章
- 疯狂安卓Android自学笔记
开发者必备自学工具: 谷歌搜索:www.yundou.info Android原版开发文档 (英文) Doc http://www.phoned.cn/docs/reference/android/v ...
- Android 读写文件
Android 读写文件 Android使用一个非常类似与其他平台上的基于磁盘的文件系统. 这节课讲述如何利用File APIs在Android文件系统中读写文件. File 对象非常适合于流式顺序数 ...
- Android 之数据存储(sdCard,sharedPreference,sqlite数据库)
sdCard:默认路径在 /storage/sdcard/... Android支持OpenFileOutput和openFileInput方式访问手机存储器上的文件. Context提供了如下两个方 ...
- To Learn
1. Hybrid:Ionic.Cordova.AngularJS等框架 webView,处理H5 2. View.ViewGroup android.view.View public class ...
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示. main.xml <?xml version="1.0" encoding=" ...
- 【Android】android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示. main.xml <?xml version="1.0" encoding="ut ...
- android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
一:第一种方式就是像Java平台下的实现方式一样通过构造器直接创建,如果需要向打开的文件末尾写入数据,可以通过使用构造器FileOutputStream(File file, boolean appe ...
- Android开发 文件读写openFileOutput与openFileInput
package com.example.androidtest; import java.io.ByteArrayOutputStream; import java.io.FileInputStrea ...
- Android 文件操作之openFileOutput
openFileOutput()方法的第一参数用于指定文件名称,不能包含路径分隔符“/” ,如果文件不存在,Android 会自动创建它.创建的文件保存在/data/data/<package ...
随机推荐
- 黄聪:定制化WordPress后台自定义仪表盘
WordPress作为一博客管理系统,相对来说已经相当简洁了,对用户也十分友好,新手也极易上手. 仪表盘是我们登陆WordPress后看到的后台界面,映入眼帘的是各种各样的信息,如WordPress ...
- Ngnix常用的操作
Nginx的常用参数如下: # /usr/local/nginx/sbin/nginx -h nginx version: nginx/0.7.63 Usage: nginx [-?hvVt] [-s ...
- 记一次在 Ubutun16.04 LTS 系统的 python-environment 安装 MySQL-python 的心(苦)路(笑)旅程
背景 之前项目需要准备线啦, 那么好了~~ 数据库也从测试时使用的 SQLITE 升级到了 MYSQL (高大上的免费且开源的关系型数据库,要不要了解一下!) 巧合的是,同事使用的是MySQL-pyt ...
- C# user32.dll
#region User32.dll 函数 /// <summary> /// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以在GDI函数中使用该句柄来在设备 ...
- come on!
团队选题与评审(团队作业 2) 队名 Rookie 团队成员的姓名与学号 211606377 覃一霸(队长) 211606346 张江波 211606371 刘治江 211606384 夏培华 211 ...
- 回到顶部最简单的JQuery实现代码
CSS代码,使用了fixed让对象固定于浏览器窗口: top{position:fixed;bottom:0;right:10px;} jQuery代码,注意正常使用的几个条件:$('#top').c ...
- Liunx cannot remove `xxx': Operation not permitted
链接: http://mangocool.com/detail_1_1439515930283.html 解到原来文件还可以设置隐藏权限,就是这个chattr设置,下面我们来看看这个命令的详解. [r ...
- 听说去了BAT的 Linuxers 都做过这套面试题!
本部分为单项选择题,每道题仅有一个答案,每题2分. 1. cron 后台常驻程序 (daemon) 用于: A. 负责文件在网络中的共享 B. 管理打印子系统 C. 跟踪管理系统信息和错误 D. ...
- pac (PAC(Proxy Auto Config) 是一个 Script;经由编写这个 Script,我们可以让系统判断在怎么样的情形下,要利用哪一台 Proxy 来进行联机。)
PAC自动代理文件格式,教你如何写PAC文件 PAC文件格式 PAC文件是纯文本格式的,实际上就是JavaScript文件.Chrome/Chromium的扩展Switchy!的"Auto ...
- epoll用法【整理】
l epoll是什么? epoll是当前在Linux下开发大规模并发网络程序的热门人选,epoll 在Linux2.6内核中正式引入,和select相似,都是I/O多路复用(IO multiplex ...