zoukankan      html  css  js  c++  java
  • 安卓存储之文件存储方式j

    支持往文件中读写数据,创建新的文件

    Main

    package com.chuanxidemo.shaoxin.demo06;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     * Created by shaoxin on 2017/2/23.
     */
    
    public class Main extends AppCompatActivity {
        private Button read;
        private Button write;
        private FileOutputStream outputStream;
    
        private FileInputStream inputStream;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            read = (Button) findViewById(R.id.read);
            write = (Button) findViewById(R.id.write);
            read.setOnClickListener(new MyClick());
            write.setOnClickListener(new MyClick());
    
        }
    
        public class MyClick implements View.OnClickListener {
    
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.write:
                        try {
                            outputStream = openFileOutput("test.txt", MODE_APPEND);
                            outputStream.write("123".getBytes());
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (outputStream != null) {
                                    outputStream.close();
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        break;
                    case R.id.read:
                        try {
                            inputStream = openFileInput("test.txt");
                            byte[] bytes = new byte[1024];
                            int num = 0;
                            int i =0;
                            while ((num = inputStream.read())!=-1){
                               bytes[i]= (byte) num;
                                i++;
                            }
                            Log.i("msg", "onClick: "+new String(bytes,0,i));
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }finally {
                            if (inputStream!=null){
                                try {
                                    inputStream.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        break;
                }
            }
        }
    }

    main.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">
    
        <Button
            android:id="@+id/read"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="读文件" />
    
        <Button
            android:id="@+id/write"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="写文件" />
    </LinearLayout>
  • 相关阅读:
    一则Entity Framework 学习中的问题
    用于主题检测的临时日志(861e835361d543a9b1b4e055dac9cf39 3bfe001a32de4114a6b44005b770f6d7)
    同一数据库如果处理多个完全不同的业务?
    swif debounce实现
    SQL Server 2008 下载地址(微软官方网站)
    fastreport 3的安装步骤
    修改windows server 2008 时间和日期格式 IIS配置后显示错误
    SQL server2000数据库备份和还原语句
    安装oracle,创建并启动oracle实例(创建克隆数据库,进度条在45%)就进行不下去了
    使用poi读写excel文件
  • 原文地址:https://www.cnblogs.com/ShaoXin/p/6432092.html
Copyright © 2011-2022 走看看