内置SD卡存取

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.EncodingUtils;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

//

public class MainActivity extends ActionBarActivity {
    
    
      private static final String TAG = null;
      private String path ;
      private String path1;
      private String content ;
      private String content1 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        path = "test.txt";
        path1 = "/sdcard/nihao.txt";
        try {
            //WriteFile(path);
            //    WriteFile1(path1);
            ReadFile(path);
            ReadFile1(path1);
        
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
     //覆盖菜单方法
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
     //写文件在 (内存)上采用OpenFileOutput方法
     public void  WriteFile(String path) throws IOException{
            
           int len = 0;
           String sentence = "sdfsdf";
           byte[]  buffer = sentence.getBytes();
           len = buffer.length;
           FileOutputStream   out  = openFileOutput(path, MODE_PRIVATE);
           //使用openFileOutput方法内存地址不能有分割符
           try {
               out.write(buffer,0 ,len);
               Log.i(TAG, "文件生成");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            out.close();
        }
         
         
     }
     //写文件在(Sdcard)上采用FileOutputStream方法 ,注意加权限
     public void WriteFile1(String path) throws IOException{
         
         FileOutputStream  fout = new FileOutputStream(path);
         byte[]  buffer = "dfgdfgdfg".getBytes();
         int len = buffer.length;
         fout.write(buffer, 0, len);
         fout.close();
         Log.i(TAG, "成功");
         
     }
     
     //读文件在(内存)上采用openFileInput的方法
      
     public  void  ReadFile(String path) throws IOException
     {
         
         String   content = null  ; 
         FileInputStream  read = openFileInput(path);
         byte[]  aaa = new byte[read.available()];
         read.read(aaa);
         content = new String(aaa);
         Log.i(TAG, content);
         
         //创建一个内存输出流对象
       /*  ByteArrayOutputStream  outstream = new ByteArrayOutputStream();
         int len = 0; 
         byte []   buffer = new byte[1024];
         try {
              while((len = read.read(buffer)) != -1){
                  outstream.write(buffer, 0, len);
              }
            content = outstream.toByteArray();
            Log.i(TAG, new String(content));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        
        }*/
      
         
     }
     
     
     //读文件在(Sdcard)上采用FileInputStream
     public void ReadFile1(String path){
             String   contentsd = "";
          try {
             
              FileInputStream  input = new FileInputStream(path);
              byte[] buffer = new byte[input.available()];
              input.read(buffer);
              contentsd = new String(buffer);
              Log.i(TAG, contentsd);
;              input.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          
        
     }
     
}

 

public class File  extends Activity{
      
      private static final String TAG = null;
      private  String filepath ;  //文件的路径
    
      public File(){
           
          
      }
    
     //写文件在 (内存)上采用OpenFileOutput
     public void  WriteFile(String path) throws IOException{
            
           int len = 0;
           String sentence = "sdfsdf";
           byte[]  buffer = sentence.getBytes();
           len = buffer.length;
           FileOutputStream   out  = openFileOutput(path, MODE_PRIVATE);
           try {
               out.write(buffer,0 ,len);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            out.close();
        }
         
         
     }
     
     
    
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值