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的更多相关文章

  1. 疯狂安卓Android自学笔记

    开发者必备自学工具: 谷歌搜索:www.yundou.info Android原版开发文档 (英文) Doc http://www.phoned.cn/docs/reference/android/v ...

  2. Android 读写文件

    Android 读写文件 Android使用一个非常类似与其他平台上的基于磁盘的文件系统. 这节课讲述如何利用File APIs在Android文件系统中读写文件. File 对象非常适合于流式顺序数 ...

  3. Android 之数据存储(sdCard,sharedPreference,sqlite数据库)

    sdCard:默认路径在 /storage/sdcard/... Android支持OpenFileOutput和openFileInput方式访问手机存储器上的文件. Context提供了如下两个方 ...

  4. To Learn

    1. Hybrid:Ionic.Cordova.AngularJS等框架 webView,处理H5 2. View.ViewGroup android.view.View  public class ...

  5. android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()

      最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示. main.xml <?xml version="1.0" encoding=" ...

  6. 【Android】android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()

    最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示. main.xml <?xml version="1.0" encoding="ut ...

  7. android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput

    一:第一种方式就是像Java平台下的实现方式一样通过构造器直接创建,如果需要向打开的文件末尾写入数据,可以通过使用构造器FileOutputStream(File file, boolean appe ...

  8. Android开发 文件读写openFileOutput与openFileInput

    package com.example.androidtest; import java.io.ByteArrayOutputStream; import java.io.FileInputStrea ...

  9. Android 文件操作之openFileOutput

    openFileOutput()方法的第一参数用于指定文件名称,不能包含路径分隔符“/” ,如果文件不存在,Android 会自动创建它.创建的文件保存在/data/data/<package ...

随机推荐

  1. ASP.NET 执行bat文件。

    参考代码: http://blog.csdn.net/youngivan/article/details/7722384 在VS开发环境上是 网站应用程序能够正常执行bat,在winServer服务器 ...

  2. WebStorm live edit 浏览器实现同步实现步骤

    1.打开WebStore的设置对话框,找到live edit选项,选中Enable live editing. 2.打开Chrome浏览器,进入Chrome网上商店,搜索JetBrains IDE S ...

  3. Windows 上用IntelliJ Idea调试百度大数据分析框架Apache Doris FE

    A. 环境准备 1. 安装jdk1.8+, Intelij IDEA 2. linux上编译好fe前端代码,主要目的是获取自动生成的代码,加入到前段工程里面去用于在idea中编译fe工程.具体编译请参 ...

  4. MySql 日期函数比较

    查询当天数据 select * from tab where FROM_UNIXTIME(fabutime, '%Y%m%d') = 20121217;   mysql TO_DAYS(date) 函 ...

  5. sql server 全文检索 使用

    目前项目中的日志查询 功能 由于长年累月的写入,目前已经达到千万级,对日志进行like 查询,速度可想而知. 此处只讨论 在数据库的优化. 当时 想到两个方案,一个是分区,一个 是全文检索. 分区的话 ...

  6. 简单的TCP代理服务器

    我之前的一篇文章(http://www.cnblogs.com/MikeZhang/archive/2012/03/07/socketRedirect.html )中介绍过用python写的一个简单的 ...

  7. Django之路 - 实现登录随机验证码

    登录验证码是每个网站登录时的基本标配,网上也有很多相应的文章, 但是从生成验证码到 应用到自己的网站上的全步骤,并没有看到很多, 为了节约大家的时间,我把整体步骤写下来, 即拿即用哈 1. 生成随机验 ...

  8. HyberLedger Fabric学习(3)-chaincode学习(开发者)

    参考:http://hyperledger-fabric.readthedocs.io/en/latest/chaincode4ade.html chaincode是由go语言写的,实现了定义的接口. ...

  9. C# 使用printDocument1.Print打印时不显示 正在打印对话框(里面还有一个讨厌的取消按钮)

    C#使用printDocument1.Print打印时不显示正在打印对话框有两种方法 第一种,使用PrintController PrintController printController = n ...

  10. Win10 安装 及应用遇到的问题

    IOS https://www.microsoft.com/zh-cn/software-download/techbench setup win10安装必须用administrator账号安装 在w ...