手机文件访问

这是在本网站的一篇博文,原文题目是

Android数据访问存储之内存读写

,但是呢,在实践的时候,运行有差别.估计是jdk版本的问题或者eclipse版本的问题.现在在我这里是完美运行了的/


package wangjun.day15;

/**
 * 在手机中进行文件的写入.还对手机中的TXT文件的内容进行编辑
 * 这个有助于以后判断手机上是否写入什么东西/
 * 但是还是有很多地方,自己还是不是很清楚.太多的专业的词.
 * 还需要加强啊
 */
import java.io.FileOutputStream;  
import java.io.IOException;  
 
import android.annotation.SuppressLint;  
import android.app.Activity;  
import android.content.Context;  
import android.os.Bundle;  
import android.os.Environment;  
import android.os.StatFs;  
import android.text.format.Formatter;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.TextView;  
import android.widget.Toast;  
 
public class MainActivity extends Activity implements OnClickListener {  
 
    private TextView tv_main_memerysize;  
    private TextView tv_main_sdcard;  
    private TextView tv_main_sdcardsize;  
      
    private EditText et_main_content;  
      
    private Button btn_main_writememery;  
    private Button btn_main_writesdcard;  
      
    private boolean sdcardMount = false;  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        tv_main_memerysize = (TextView) findViewById(R.id.sj_neicun_tv);  
        tv_main_sdcard = (TextView) findViewById(R.id.sd_cunzai_tv);  
        tv_main_sdcardsize = (TextView) findViewById(R.id.sd_neicun_tv);  
        et_main_content = (EditText) findViewById(R.id.csxieru_et);  
        btn_main_writememery = (Button) findViewById(R.id.xieru_btn);  
        btn_main_writesdcard = (Button) findViewById(R.id.xrsd_btn);  
          
        btn_main_writememery.setOnClickListener(this);  
        btn_main_writesdcard.setOnClickListener(this);  
          
        //判断sdcard是否已安装  
        tv_main_sdcard.setText("sdcard未安装");  
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){  
            sdcardMount = true;  
            tv_main_sdcard.setText("sdcard已安装");  
        }  
          
        //得到sdcard和内存的大小及可用容量  
        if(sdcardMount){  
            tv_main_sdcardsize.setText("sdcard" + getStorgeFileSize(Environment.getExternalStorageDirectory().getPath()));  
        }else {  
            tv_main_sdcardsize.setText("sdcard总内存:0MB;  可用内存:0MB");  
        }  
        tv_main_memerysize.setText("手机" + getStorgeFileSize(Environment.getDataDirectory().getPath()));  
          
    }  
 
    /* 两个按钮的监听事件,将内容写入内存文件testmemeryio.txt  */  
    @Override  
    public void onClick(View v) {  
        String text = et_main_content.getText().toString();  
        boolean success = true;  
          
        switch (v.getId()) {  
            case R.id.xrsd_btn: //写入sdcard  
                FileOutputStream fos = null;  
                try {  
                    fos = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/m.txt");  
                    fos.write(text.getBytes("utf-8"));  
                    fos.flush();  
                } catch (IOException e) {  
                    success = false;  
                    e.printStackTrace();  
                } finally {  
                    if(fos != null){  
                        try {  
                            fos.close();  
                            fos = null;  
                        } catch (IOException e) {  
                            e.printStackTrace();  
                        }  
                    }  
                }  
                Toast.makeText(MainActivity.this, success == true ? "写入sdcard文件成功" : "写入sdcard文件失败", 0).show();  
                break;  
              
            case R.id.xieru_btn: //写入内存  
                FileOutputStream openFileOutput = null;  
                  
                try {  
                    //使用openFileOutput()函数,直接在/data/data/包名/files/目录下创建文件  
                    openFileOutput = openFileOutput("testmemeryio.txt", Context.MODE_PRIVATE);  //私有模式写文件  
                    openFileOutput.write(text.getBytes("utf-8"));  
                    openFileOutput.flush();  
                } catch (IOException e) {  
                    success = false;  
                    e.printStackTrace();  
                } finally {  
                    if(openFileOutput != null){  
                        try {  
                            openFileOutput.close();  
                            openFileOutput = null;  
                        } catch (IOException e) {  
                            e.printStackTrace();  
                        }  
                    }  
                }  
                Toast.makeText(MainActivity.this, success == true ? "写入内存文件成功" : "写入内存文件失败", 0).show();  
                break;  
              
            default:  
                break;  
        }  
    }  
      
    /* 取得内存文件空间大小及可用大小 */  
    @SuppressLint("NewApi") private String getStorgeFileSize(String path){  
        String fileSizeDesc = null;  
          
        StatFs statFs = new StatFs(path);                           //获得磁盘状态的对象  
        long blockSizeLong = statFs.getBlockSize();             //获得磁盘一个扇区的大小  
        long blockCountLong = statFs.getBlockCount();           //获得磁盘空间总的扇区数  
        long availableBlocksLong = statFs.getAvailableBlocks(); //获得磁盘空间总的可用扇区数  
        fileSizeDesc = "总内存:" + Formatter.formatFileSize(MainActivity.this, blockSizeLong*blockCountLong)  
                     + "; 可用内存:" + Formatter.formatFileSize(MainActivity.this, blockSizeLong*availableBlocksLong);  
          
        return fileSizeDesc;  
    }  
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值