保存数据到SD卡的封装

来源:互联网 发布:sql建一个销售表 编辑:程序博客网 时间:2024/05/17 04:14

封装类:

package com.example.testsave.util;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import android.content.Context;import android.os.Environment;import android.util.Log;import android.widget.Toast;public class SaveUtil {    /**     *      * @param filename     * @param content     * @throws Exception     */    public static void saveToSDCard(String filename, String content)            throws Exception {        File file = new File(Environment.getExternalStorageDirectory(),                filename);// 指定文件存储目录为SD卡,文件名        FileOutputStream outStream = new FileOutputStream(file);// 输出文件流        outStream.write(content.getBytes());        outStream.close();        //如果没有a.txt会自动创建  加入true 会每次都追加  不加的话 则直接是新的文件 把a.txt替换掉  //        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "a.txt");  //        FileOutputStream fos = new FileOutputStream(file, true);  //        fos.write("这个是存储数据".getBytes());  //        //这个地方并没有必要调用这个刷新方法 因为close也自带close 但是要养成好习惯  //        fos.flush();  //        fos.close();      }    /**     *      * @param context     * @return  true 存在   false 不存在     */    public static boolean isExist(Context context) {        boolean isExist = false;        try {            // 判断SDCard是否存在,并且可以读写            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {                Toast.makeText(context, "yes", 1).show();                isExist = true;            } else {                Toast.makeText(context, "no", 1).show();                isExist = false;            }        } catch (Exception e) {            Toast.makeText(context, "no", 1).show();            e.printStackTrace();        }        return isExist;    }    /**     *      * @param file     * @throws Exception     */    public static void getToSDDate(String fileName) throws Exception{        File file = new File(Environment.getExternalStorageDirectory(),                fileName);        FileInputStream fis = new FileInputStream(file);          BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis));          String s = bufferedReader.readLine();          //这个地方会显示这个是存储数据          Log.e("MainActivity", s);        fis.close();      }}

调用:

package com.example.testsave;import com.example.testsave.util.SaveUtil;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        boolean exist = SaveUtil.isExist(getApplicationContext());        if (exist) {            try {                SaveUtil.saveToSDCard("1.txt", "test");  // 保存数据            } catch (Exception e) {                e.printStackTrace();            }        }        try {            SaveUtil.getToSDDate("1.txt");   // 根据名称获取保存的数据        } catch (Exception e) {            e.printStackTrace();        }//        Log.e("MainActivity", localUrl);    }}

网络下载文件的保存:
http://blog.csdn.net/pengyu1801/article/details/50085909

http://blog.csdn.net/pengyu1801/article/details/50085761

0 0
原创粉丝点击