首页 技术 正文
技术 2022年11月9日
0 收藏 541 点赞 2,995 浏览 3663 个字

Android设计了一套可以操作自身APP目录文件对API openFileInput openFileOutput,读取只需传入文件名,写入需要传入文件名 与 权限模式

界面:

Android-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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903