界面:

Layout:

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

MainActivity读写相关代码:

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.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; public class MainActivity3 extends Activity implements View.OnClickListener { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3); initViewListener();
} 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: {
// Java传统方式 注意:访问自身APP到目录是不需要权限 Linux目录是以/为开头
File file = new File("/data/data/" + getPackageName() + "/my_file.txt");
if (!file.exists()) {
Toast.makeText(MainActivity3.this, "文件不存在", Toast.LENGTH_LONG).show();
return;
}
try {
// 使用字符读取流
FileReader fileReader = new FileReader(file);
// 为什么要用BufferedReader,因为BufferedReader有readLine()的方法
BufferedReader br = new BufferedReader(fileReader);
String result = br.readLine();
tvInput.setText(result + ""); fileReader.close();
br.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;
} // Java传统方式 注意:访问自身APP到目录是不需要权限 Linux目录是以/为开头
File file = new File("/data/data/" + getPackageName() + "/my_file.txt");
try {
// 字符写入流
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(outputStr);
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
default:
break;
}
}
}

存储的目录:

Android-Java读写文件到自身APP目录的更多相关文章

  1. 在android中读写文件

    在android中读写文件 android中只有一个盘,正斜杠/代表根目录. 我们常见的SDK的位置为:/mnt/sdcard 两种最常见的数据存储方式: 一.内存 二.本地 1.手机内部存储 2.外 ...

  2. Java读写文件方法总结

    Java读写文件方法总结 Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便.奈何我的记性实在是叫人着急,很多时候既 ...

  3. Java读写文件的几种方式

    自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...

  4. java读写文件大全

     java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...

  5. 【java】java 读写文件

    场景:JDK8  将上传的文件,保存到服务器 Java读写文件操作: MultipartFile file InputStream inputStream = file.getInputStream( ...

  6. Android的读写文件权限

    设置文件生成的权限: public static boolean saveInfo( Context context, String userName, String userPass, int mo ...

  7. django中,如何把所有models模型文件放在同一个app目录下?

    django的每个app目录下,都有自己的models.py文件. 原则上,每个app涉及的数据库,都会定义在这个文件里. 但是,有的数据库,涉及到多个app应用,不是很方便放在一个单独的app里. ...

  8. 转:Java读写文件各种方法及性能比较

    干Java这么久,一直在做WEB相关的项目,一些基础类差不多都已经忘记.经常想得捡起,但总是因为一些原因,不能如愿. 其实不是没有时间,只是有些时候疲于总结,今得空,下定决心将丢掉的都给捡起来. 文件 ...

  9. Java读写文件常用方法

    一.字符流:读写纯文本(txt,csv等), 1 字符流写文件主要用:FileWriter,BufferedWriter,PrintWriter 1.1 测试 FileWriter 写入 privat ...

随机推荐

  1. 转 shell 命令 http://www.cnblogs.com/me115/p/3427319.html

    http://www.cnblogs.com/me115/p/3427319.html 本文将介绍Linux下使用Shell处理文本时最常用的工具:find.grep.xargs.sort.uniq. ...

  2. iis 更改asp.net 版本设置

    参考来源: https://github.com/neo2018/ZYFC/blob/2e20009097c1e837a6e667a3dffd4224e28f4411/MderFc/Classes/I ...

  3. Linux操作系统-基本命令(二)

    Linux操作系统基本命令 文件操作类命令 – ln命令 另外一种链接方式称为符号链接(软链接),是指一个文件指向另外一个文件的文件名.软链接类似于Windows系统中的快捷方式.软链接由ln -s命 ...

  4. Python中小整数对象池和大整数对象池

    1. 小整数对象池 整数在程序中的使用非常广泛,Python为了优化速度,使用了小整数对象池, 避免为整数频繁申请和销毁内存空间. Python 对小整数的定义是 [-5, 256] 这些整数对象是提 ...

  5. 第七章 : Git 介绍 (下)[Learn Android Studio 汉化教程]

    Learn Android Studio 汉化教程 Let’s reset even further to remove all traces of your work on the deprecat ...

  6. 学习了django对于sqlite3进行了了解,谈谈看法

    学习了django对于sqlite3进行了了解,谈谈看法 由于django默认使用的是sqlite3,写了几个建表语句, 然后数据做下迁移,其实就是建表语句的执行. 一直对sqlite3没有一个直观的 ...

  7. javascript的中的new

    考察 ECMAScript 语言规范中 new 运算符的定义: The new Operator The production NewExpression : new NewExpression is ...

  8. 新的方法(Set<T>)实现mvc的crud

    model层的属性为: public partial class UserInfo { public int Uid { get; set; } public string UName { get; ...

  9. MPI 集合通信函数 MPI_Scatterv(),MPI_Gatherv(),MPI_Allgatherv(),MPI_Alltoall(),MPI_Alltoallv(),MPI_Alltoallw()

    ▶ 函数 MPI_Scatterv() 和 MPI_Gatherv() .注意到函数 MPI_Scatter() 和 MPI_Gather() 只能向每个进程发送或接受相同个数的元素,如果希望各进程获 ...

  10. Spring cloud Hystrix使用@HystrixCommand使用Hystrix组件及@EnableCircuitBreaker原理介绍

    通过@HystrixCommand注解实现在Spring Cloud使用Hystrix组件相关的工程 cloud-registration-center:注册中心 cloud-service-hyst ...