首页 > 其他分享 >2022-11-2学习内容

2022-11-2学习内容

时间:2022-11-02 22:00:47浏览次数:79  
标签:11 sb String 学习 2022 import et id append

1.外部存储空间

1.1FileWriteActivity.java

package com.example.chapter06;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.example.chapter06.util.FileUtil;
import com.example.chapter06.util.ToastUtil;

import java.io.File;

public class FileWriteActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private CheckBox ck_married;
    private String path;
    private TextView tv_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_write);
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);
        ck_married = findViewById(R.id.ck_married);
        tv_text = findViewById(R.id.tv_text);

        findViewById(R.id.btn_save).setOnClickListener(this);
        findViewById(R.id.btn_read).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save:
                String name = et_name.getText().toString();
                String age = et_age.getText().toString();
                String height = et_height.getText().toString();
                String weight = et_weight.getText().toString();

                StringBuilder sb = new StringBuilder();
                sb.append("姓名:").append(name);
                sb.append("\n年龄:").append(age);
                sb.append("\n身高:").append(height);
                sb.append("\n体重:").append(weight);
                sb.append("\n婚否:").append(ck_married.isChecked() ? "是" : "否");

                String fileName = System.currentTimeMillis() + ".txt";
                String directory = null;
                // 外部存储的公共空间
//                directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();

                // 内部存储私有空间
                directory = getFilesDir().toString();
                path = directory + File.separatorChar + fileName;
                Log.d("ning", path);
                FileUtil.saveText(path, sb.toString());
                ToastUtil.show(this, "保存成功");
                break;
            case R.id.btn_read:
                tv_text.setText(FileUtil.openText(path));/*1350*/
                break;
        }
    }
}

1.2FileUtil.java

package com.example.chapter06.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtil {

    // 把字符串保存到指定路径的文本文件
    public static void saveText(String path, String txt) {
        BufferedWriter os = null;
        try {
            os = new BufferedWriter(new FileWriter(path));
            os.write(txt);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 从指定路径的文本文件中公读取内容字符串
    public static String openText(String path) {
        BufferedReader is = null;
        StringBuilder sb = new StringBuilder();
        try {
            is = new BufferedReader(new FileReader(path));
            String line = null;
            while((line = is.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
}

1.3activity_file_write.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="姓名:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:background="@drawable/editext_selector"
            android:hint="请输入姓名"
            android:inputType="text"
            android:maxLength="12"
            android:textColor="@color/black"
            android:textSize="17sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_age"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="年龄:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_age"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:background="@drawable/editext_selector"
            android:hint="请输入年龄"
            android:inputType="number"
            android:maxLength="2"
            android:textColor="@color/black"
            android:textSize="17sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_height"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="身高:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_height"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:background="@drawable/editext_selector"
            android:hint="请输入身高"
            android:inputType="number"
            android:maxLength="3"
            android:textColor="@color/black"
            android:textSize="17sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_weight"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="体重:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_weight"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:background="@drawable/editext_selector"
            android:hint="请输入体重"
            android:inputType="numberDecimal"
            android:maxLength="5"
            android:textColor="@color/black"
            android:textSize="17sp" />

    </LinearLayout>

    <CheckBox
        android:id="@+id/ck_married"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:gravity="center"
        android:text="已婚"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <Button
        android:id="@+id/btn_read"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="读取"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="17sp" />

</LinearLayout>

1.4效果:

 保存到外部存储的公共空间,控制台输出:

D/ning: /storage/emulated/0/Download/1667391285238.txt

效果:

 

 文件内容:

 

点“读取”,效果:

 

 

 同时,SD卡路径下也有这个文件:

 

 卸载APP后,可以看到,存储在外部存储公共空间的文件仍存在:

 

 保存到内部存储私有空间,控制台日志:

D/ning: /data/user/0/com.example.chapter06/files/1667392095069.txt

效果:

 

 文件内容:

 

 点击“读取”:

 

 卸载APP后,可以发现内部存储私有空间的文件也没了:

 

标签:11,sb,String,学习,2022,import,et,id,append
From: https://www.cnblogs.com/pingfanliliang/p/16849777.html

相关文章

  • 2022_11_2
    ......
  • 学习笔记:KMP
    引入KMP是一种字符串匹配算法,可以在将近线性的时间复杂度内进行字符串匹配。此类问题通常有一个文本串$S$和一个模式串$P$构成,说白了就是在$S$中匹配$T$,S.find(T)......
  • 【2022.11.2】Vue基础学习(7)
    内容详细1vue3介绍1.性能的提升打包大小减少41%初次渲染快55%,更新渲染快133%内存减少54%2.源码的升级使用Proxy代替defineProperty实现响应式......
  • IPV6的简单学习与整理
    背景大概2018年时曾经突击学习过一段时间IPV6当时没太有写文档的习惯,导致这边没有成型的记录了.今天又有项目要求使用IPV6,想了想就将之前学习的部分还有想继续学习......
  • 核磁共振成像学习笔记——基本加权成像方式
    对核磁共振成像而言,最为基本的加权成像包括T1-weighted(T1W),T2-weighted(T2W),protondensity(PDW)。T1:是所谓的纵向弛豫时间,就是说你把质子磁化弄到z轴负向后,他要花......
  • CSP2022 游寄
    今年还是很寄考场上感觉真的没脑子。上来\(T1\)就给我整蒙了,想到原来看过的三元环计数,偏了然后在四个题之间反复横跳,觉得T2应该可做,策略就是取几个最值,T3比较离谱,T4想......
  • 11岁男孩跪地守候病危妈妈,父亲10年不归家直接挂断电话,孩子的未来怎么办?
    近日,网传一段“安徽阜阳临泉县11岁男孩独自跪地守候患癌母亲”的视频,令人泪目。打开百度APP,查看更多高清图片视频来自2020年11月21号,在安徽阜阳,患癌病危的母亲抚摸着11岁儿......
  • 美国9·11事件永远不能忘记的22张照片(内附高清大图)
    文/王不留(微信公众号:王不留)9·11事件离我们已过去二十年了。那是21世纪初最重要的突发事件。虽然地点发生在美国,但以史为镜,可以知兴替,我们更应避免这种事情的发生。恐怖主......
  • vue学习笔记
    今日内容概要vue3介绍创建vue3项目的方式setup函数ref和reactive计算和监听属性生命周期hookstoRefs后台管理模板今日内容详细vue3介绍1.性能的提升......
  • 待学习内容记录
    目录待学习内容cross-storage已学习内容--待学习内容cross-storage已学习内容--......