标签 example 下的文章

Android文件存储


其实所用到的核心技术就是 Context类中提的 openFileInput()和 openFileOutput()方法,之后就是利用 Java 的各种流来进行读写操作就可以了。按书上写了个demo:

package com.project.ics.day0820filepersistencetest;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends Activity {

    EditText editText;

    Button btn;
    String textString;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText= (EditText) findViewById(R.id.et_edit);

        btn= (Button) findViewById(R.id.btn_save);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputText = editText.getText().toString();
                save(inputText);
            }
        });



        String inputText=load();
       // Toast.makeText(this,inputText,Toast.LENGTH_SHORT).show();
        if(!TextUtils.isEmpty(inputText)){
            editText.setText(inputText);
            editText.setSelection(inputText.length());
            Toast.makeText(this,"Restoring succeeded",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"It was empty!",Toast.LENGTH_SHORT).show();
        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    public void save(String inputText) {
        FileOutputStream out =null;
        BufferedWriter writer = null;
        try {
            out = openFileOutput("data", Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(inputText);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public String load(){
        String lineString="";
        FileInputStream in=null;
        BufferedReader reader=null;

        StringBuilder line=new StringBuilder();
        try {
            in=openFileInput("data");
            reader=new BufferedReader(new InputStreamReader(in));
            while((lineString=reader.readLine())!=null){
                line.append(lineString);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return line.toString();
    }

}

较为复杂的文本数据不过文件存储的方式并不适合用于保存一些较为复杂的文本数据,更复杂点的文本数据,可以用SharedPreferences。SharedPreferences是通过键值对的方式来存储数据的。


IO流例子编写




无标题

后缀是Stream是字节流,而后缀是ReaderWriter是字符流。
Jdk提供的流继承了四大类:InputStream(字节输入流)OutputStream(字节输出流),Reader(字符输入流),Writer(字符输出流)。

按字节来读取文件
  1. public class ReadFromFile {
  2.     /**
  3.      * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
  4.      */
  5.     public static void readFileByBytes(String fileName) {
  6.         File file = new File(fileName);
  7.         InputStream in = null;
  8.         try {
  9.             System.out.println("以字节为单位读取文件内容,一次读一个字节:");
  10.             // 一次读一个字节
  11.             in = new FileInputStream(file);
  12.             int tempbyte;
  13.             while ((tempbyte = in.read()) != -1) {
  14.                 System.out.print(tempbyte);
  15.             }
  16.             in.close();
  17.         } catch (IOException e) {
  18.             e.printStackTrace();
  19.             return;
  20.         }



按行来读取文件
  1. /**
  2.      * 以行为单位读取文件,常用于读面向行的格式化文件
  3.      */
  4.     public static void readFileByLines(String fileName) {
  5.         File file = new File(fileName);
  6.         BufferedReader reader = null;
  7.         try {
  8.             System.out.println("以行为单位读取文件内容,一次读一整行:");
  9.             reader = new BufferedReader(new FileReader(file));
  10.             String tempString = null;
  11.             int line = 1;
  12.             // 一次读入一行,直到读入null为文件结束
  13.             while ((tempString = reader.readLine()) != null) {
  14.                 // 显示行号
  15.                 System.out.println("line " + line + ": " + tempString);
  16.                 line++;
  17.             }
  18.             reader.close();
  19.         } catch (IOException e) {
  20.             e.printStackTrace();
  21.         } finally {
  22.             if (reader != null) {
  23.                 try {
  24.                     reader.close();
  25.                 } catch (IOException e1) {
  26.                 }
  27.             }
  28.         }
  29.     }



一个文件读到另一个文件
  1. public static void FiletoAnotherFile(String fileName){
  2. File file=new File(fileName);
  3. BufferedReader reader=null;
  4. BufferedWriter writer=null;
  5. try {
  6. writer=new BufferedWriter(new FileWriter("D://wu2.txt"));
  7. } catch (IOException e) {
  8. // TODO Auto-generated catch block
  9. e.printStackTrace();
  10. }
  11. try {
  12. reader=new BufferedReader(new FileReader(file));
  13. String tempString=null;
  14. while((tempString=reader.readLine())!=null){
  15. writer.append(tempString);
  16. writer.newLine();
  17. writer.flush();
  18. }
  19. } catch (FileNotFoundException e) {
  20. // TODO Auto-generated catch block
  21. e.printStackTrace();
  22. } catch (IOException e) {
  23. // TODO Auto-generated catch block
  24. e.printStackTrace();
  25. }finally{
  26. try {
  27. reader.close();
  28. writer.close();
  29. System.out.println("write done");
  30. } catch (IOException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35. }






shijiebei 365bet manbetx 188bet xinshui caipiao 95zz tongbaoyule beplay 88bifa 18luck betway bwin hg0088 aomenjinshayulecheng ca88 shenbotaiyangcheng vwin w88 weide