文件存储---内部存储/外部存储_网站外置文件存储-程序员宅基地

技术标签: android  

一个简单的记事本   

外部存储需要内存卡读取写入权限

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context="com.ccyumo.demo.MainActivity">

    <EditText
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:background="#ffa200"
        android:gravity="top|left"
        android:scrollbars="vertical"
        android:text="alertDialog"
        android:padding="10dp"
        android:layout_marginBottom="10dp"/>
    <Button
        android:id="@+id/save_btn"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:background="@color/colorPrimary"
        android:text="保存"/>


</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    EditText content;
    Button save_btn;
    //File file=; 外部存储代码
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        content = (EditText) findViewById(R.id.content);
        save_btn = (Button) findViewById(R.id.save_btn);
        //file=new File(Environment.getExternalStorageDirectory(),"text.text");
        byte buffer[]=null;
        FileInputStream fi = null;
        save_btn.setOnClickListener(new myClick());
        try {
            fi = openFileInput("Demo");//外部存储改为 fi = new FileInputStream(file);
            buffer = new byte[fi.available()];
            fi.read(buffer);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fi != null) {
                try {
                    fi.close();
                    String cont=new String(buffer);
                    content.setText(cont);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    //保存按钮监听事件
    public class myClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            String con = content.getText().toString();
            FileOutputStream fileOutputStream = null;
            //保存文件
            try {
                fileOutputStream = openFileOutput("Demo", MODE_PRIVATE);//外部存储改为  fileOutputStream = new FileOutputStream(file);
                fileOutputStream.write(con.getBytes());
                fileOutputStream.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_44174689/article/details/86000644

智能推荐

正点原子bootloader代码(STM32F103ZET6)粗略解读_正点原子f103 bootloader模式-程序员宅基地

文章浏览阅读943次,点赞19次,收藏18次。关于正点原子IAP程序解读_正点原子f103 bootloader模式

多目标麻雀搜索优化算法及其MATLAB实现_多目标麻雀优化-程序员宅基地

文章浏览阅读186次。麻雀搜索优化算法(Sparrow Search Optimization,简称SSO)是一种基于麻雀行为的启发式优化算法,用于解决多目标优化问题。该算法模拟了麻雀在觅食和寻找栖息地过程中的行为,通过群体合作和信息共享来寻找最优解。本文将介绍多目标麻雀搜索优化算法的原理,并提供MATLAB实现的源代码。本文介绍了多目标麻雀搜索优化算法的原理,并提供了MATLAB实现的源代码。通过模拟麻雀的觅食和寻找栖息地行为,该算法在解决多目标优化问题上具有一定的效果。使用时,可以根据具体的多目标优化问题,实现。_多目标麻雀优化

newFixedThreadPool、newSingleThreadPool、newCachedThreadPool线程池创建线程的方式_executors.newfixedthreadpool(5)如何创建多线程-程序员宅基地

文章浏览阅读411次。newFixedThreadPool、newSingleThreadPool、newCachedThreadPool线程池创建线程的方式_executors.newfixedthreadpool(5)如何创建多线程

Java接口幂等性设计场景解决方案v1.0_java 面试场景题解决方案方案-程序员宅基地

文章浏览阅读145次,点赞2次,收藏2次。用户对于同一操作发起的一次请求或者多次请求的结果是一致的,不会因为多次点击而产生了副作用。举个简单的例子:那就是支付,用户购买商品后支付,支付扣款成功,但是返回结果的时候网络异常了,此时钱已经扣了,用户再次点击按钮,此时会进行第二次扣款,返回结果成功,用户查询余额发现多扣钱了,流水记录也变成了两条。在以前的单应用系统中,我们只需要对数据操作加入事务即可,发生错误的时候立即回滚,但是再响应客户端的时候也有可能网络中断或者异常等等情况。_java 面试场景题解决方案方案

A USB HID Component for C#-程序员宅基地

文章浏览阅读5.5k次。 Download source - 262.48 KB IntroductionThis article is about a USB HID component which enables you to communicate with HID devices over USB. There is no default component available for USB_a usb hid component for c#

SpringCloudOAuth2中访问/oauth/token报server_error_spring cloud oauth2中访问 /oauth/token 跳转不到具体方法-程序员宅基地

文章浏览阅读2k次。问题分析在新建的Spring Cloud OAuth2项目中使用grant_type为password方式访问时报server_error。在postman中如下图:{ "error": "server_error", "error_description": "Internal Server Error"}java后台报错如下:endpoint.TokenEndpoint : Handling error: NestedServletException, Handler d_spring cloud oauth2中访问 /oauth/token 跳转不到具体方法

随便推点

python 图像处理就业_python+cv环境下的图像处理-程序员宅基地

文章浏览阅读126次。一、直方图1.1原理直方图是数值数据分布的精确图形表示。 这是一个连续变量的概率分布的估计,是一种条形图。为了构建直方图,第一步是将值的范围分段,即将整个值的范围分成一系列间隔,然后计算每个间隔中有多少值。 这些值通常被指定为连续的,不重叠的变量间隔。 间隔必须相邻,并且通常是(但不是必须的)相等的大小。在画图像轮廓前需要将原图像转换为灰度图像,因为轮廓需要获取每个坐标[x,y]位置的像素值。1...._im = array(image.open(img_path).convert('l')) typeerror: array() argument 1

java http 请求网页保存导入写入 本地 文件_java http接口获取数据,结果写入文件-程序员宅基地

文章浏览阅读828次。目录依赖调用获取网页写入本地import依赖<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5.2</version..._java http接口获取数据,结果写入文件

lists这个类无法使用maven打包_lists maven-程序员宅基地

文章浏览阅读1.6k次。org.assertj assertj-core_lists maven

关于微信小程序打包文件vendor.js超过500k的压缩方案_发行时压缩vendor文件-程序员宅基地

文章浏览阅读5.4k次。关于微信小程序打包文件vendor.js超过500k的压缩方案因为是开发环境,所以没进行UglifyJs压缩,所以解决的方法来了,引入UglifyJs插件修改build目录下 的webpack.dev.conf.js配置文件,前面添加插件的引入,var UglifyJsPlugin = require('uglifyjs-webpack-plugin') // 在插件列表加上一句话,就可将..._发行时压缩vendor文件

C语言--指针详解(下)--字符指针、数组指针、指针数组、函数指针、函数指针数组(转移表)-程序员宅基地

文章浏览阅读1k次,点赞33次,收藏30次。字符指针、数组指针、指针数组、函数指针、函数指针数组涵盖了在指针学习中有关指针的绝大多数的情况,熟练掌握它们,将对学习指针有巨大的帮助。同时,指针部分是C语言学习中重要的部分之一,熟练掌握指针对于C语言学习来说很重要。

心理韧性与青少年的大脑结构、功能和连接-程序员宅基地

文章浏览阅读31次。尽管早期的逆境经历与日后精神病理学风险增加有关,但一些经历过童年期逆境的个体表现出心理韧性。目前对心理韧性的神经相关知之甚少,特别是在青少年群体中。为了填补这一空白,我们对青少年心理韧性的神经影像学研究进行了系统综述。我们检索了PubMed、Web of Science、Scopus和PsycINFO数据库,共确定了5,482项研究。通过筛选标题/摘要,并通读剩余文章,纳入了基于19个独特数据集的22项研究。我们发现初步证据表明,通过结构和功能MRI以及弥散张量成像方法评估,心理韧性与青少年的大脑结构、功能

推荐文章

热门文章

相关标签