zoukankan      html  css  js  c++  java
  • Android-Java读写文件到自身APP目录

    界面:

    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;
            }
        }
    }

    存储的目录:

  • 相关阅读:
    移动端滑动效果
    使用Bash时的几点总结
    docker-It's possible that too few managers are online. Make sure more than half of the managers are online.
    基于elk 实现nginx日志收集与数据分析。
    python-num18 - django进阶一
    文成小盆友python-num17 - django基础
    文成小盆友python-num15 - JavaScript基础
    文成小盆友python-num14 - web 前端基础 html ,css, JavaScript
    文成小盆友python-num13 整个堡垒机
    install pip3 for python 3.x
  • 原文地址:https://www.cnblogs.com/android-deli/p/10087580.html
Copyright © 2011-2022 走看看