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();

                  }

     

               }

           });

     

        }

    }

  • 相关阅读:
    C#实现根据域名查询ip实例
    Ajax: 一个建立Web应用的新途径(转)
    CRC循环校验的具体算法(转)
    生成静态文件的新闻系统核心代码(.net C#)
    一个ajax的例子
    使用 JavaScript 实现 XMLHttpRequest,在IE,FireFox 上测试通过
    微软SQL Server 2005的30项顶尖特性(转)
    利用XMLHTTP无刷新自动实时更新数据(转)
    五子棋的核心算法(转)
    编写安全的SQL Server扩展存储过程(转)
  • 原文地址:https://www.cnblogs.com/riskyer/p/3283449.html
Copyright © 2011-2022 走看看