zoukankan      html  css  js  c++  java
  • android中file的使用实例

    File是android的4种存储方式的一种。File就是文件的意思一个文件,你无非是想进行读写操作。所以这就用到两个流。一个数输入流,一个是输出流。FileOutstream,和FileInputSream。这两个和java基础的流是一样的。如果你对流不清楚可以我看看我以前写的java对流的使用。

    下面我写一个android程序对file是的使用

    布局

    下面我就写一个actitvty的代码

    package com.android.app;

     

    import java.io.FileInputStream;

    import java.io.FileNotFoundException;

    import java.io.FileOutputStream;

    import java.io.IOException;

     

    import android.app.Activity;

    import android.content.Context;

    import android.os.Bundle;

    import android.view.View;

    import android.view.View.OnClickListener;

    import android.widget.Button;

    import android.widget.CheckBox;

    import android.widget.EditText;

    import android.widget.TextView;

     

    public class File extends Activity {

        /** Called when the activity is firstcreated. */

        private EditText content;

        private TextView readContent;

        private Button writeButton;

        private Button readButton;

        private CheckBox checkBox;

        private TextView showContent;

     

        @Override

        public void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.main);

           //绑定所有组件

           content = (EditText) findViewById(R.id.EditText01);

           readButton = (Button) findViewById(R.id.Button02);

           writeButton = (Button) findViewById(R.id.Button01);

           checkBox = (CheckBox) findViewById(R.id.CheckBox01);

           showContent = (TextView) findViewById(R.id.TextView01);

           readContent = (TextView) findViewById(R.id.TextView02);

     

          

           //点击写按钮触发写入事件

           writeButton.setOnClickListener(new OnClickListener() {

     

               @Override

               public void onClick(View v) {

                  FileOutputStream fos = null;

                  try {

                      if (checkBox.isChecked()) {

                         //这个就是一个流文件的写入第一个参数是一个文件的文件的文件名,后面的文件的写入方式

                         fos = openFileOutput("fish", Context.MODE_APPEND);//追加的写入

                      } else {

                         fos = openFileOutput("fish", Context.MODE_PRIVATE);//覆盖的写入

                      }

     

                      String text = content.getText().toString();

                      fos.write(text.getBytes());   //将你输入的东西写入缓冲区。这里还没有在文件里面写

                      showContent.setText("写入成功");

                      content.setText("");

     

                  } catch (FileNotFoundException e) {

                      // TODO Auto-generatedcatch block

                      e.printStackTrace();

                  } catch (IOException e) {

                      // TODO Auto-generatedcatch block

                      e.printStackTrace();

                  } finally {

                      if (fos != null) {

                         try {

                             fos.flush();//当执行完这个你的文件里面才有你写入的内容

                             fos.close();

                         } catch (IOException e) {

                             // TODO Auto-generatedcatch block

                             e.printStackTrace();

                         }

     

                      }

                  }

               }

           });

          

          

           //点击读按钮触发读入事件

           readButton.setOnClickListener(new OnClickListener() {

     

               @Override

               public void onClick(View v) {

                  readContent.setText("");

                  FileInputStream fis = null;

                  try {

                      fis = openFileInput("fish");

                      if (fis.available() == 0) {

                         return;

                      } else {

                         byte[] con = new byte[fis.available()];

                         while (fis.read(con) != -1) {

                         }

                         readContent.setText(new String(con));

                          showContent.setText("读取成功");

                      }

                  } catch (FileNotFoundException e) {

                      // TODO Auto-generatedcatch block

                      e.printStackTrace();

                  } catch (IOException e) {

                      // TODO Auto-generatedcatch block

                      e.printStackTrace();

                  }

     

               }

           });

     

        }

    }

  • 相关阅读:
    python常识系列17-->利用Faker模块造测试数据
    python常识系列16-->python自带的Mock模块使用
    python常识系列15-->python利用xlrd处理合并单元格
    python常识系列14-->python通过jpype模块调用jar包
    杂七杂八的问题处理01--mac下的eclipse默认不提供代码联想功能
    httprunner踩坑记03-->debugtalk.py中的方法传参
    httprunner踩坑记02-->利用parameters参数进行参数化
    vue新建项目一直在downloading template转,最后超时
    vue图片加载出错显示默认占位图片
    修改input复选框样式
  • 原文地址:https://www.cnblogs.com/riskyer/p/3283449.html
Copyright © 2011-2022 走看看