android 创建 并且 读取文件

  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6.   
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.os.Environment;  
  10. import android.util.Log;  
  11. import android.widget.TextView;  
  12.   
  13. /** 
  14.  * This activity shows how to write and read data from sdcard 
  15.  * @author FaYnaSoft Labs 
  16.  * 
  17.  */  
  18. public class Main extends Activity {  
  19.     private static final String APP_TAG = "tag";  
  20.   
  21.     private TextView readOutput;  
  22.   
  23.     @Override  
  24.     public void onCreate(final Bundle icicle) {  
  25.         super.onCreate(icicle);  
  26.         this.setContentView(R.layout.main);  
  27.   
  28.         readOutput = (TextView) findViewById(R.id.output);  
  29.   
  30.         String fileName = "test-" + System.currentTimeMillis() + ".txt";  
  31.   
  32.         // create /sdcard/test_folder  
  33.         File sdDir = new File(Environment.getExternalStorageDirectory().getPath());  
  34.         if (sdDir.exists() && sdDir.canWrite()) {  
  35.             File testDir = new File(sdDir.getAbsolutePath() + "/test_folder");  
  36.             testDir.mkdir();  
  37.             if (testDir.exists() && testDir.canWrite()) {  
  38.                 File file = new File(testDir.getAbsolutePath() + "/" + fileName);  
  39.                 try {  
  40.                     file.createNewFile();  
  41.                 } catch (IOException e) {  
  42.                     Log.e(APP_TAG, "error creating file", e);  
  43.                 }  
  44.   
  45.                 if (file.exists() && file.canWrite()) {  
  46.                     FileOutputStream fos = null;  
  47.                     try {  
  48.                         fos = new FileOutputStream(file);  
  49.                         fos.write("Hello, World!".getBytes());  
  50.                     } catch (FileNotFoundException e) {  
  51.                         Log.e(APP_TAG, "ERROR", e);  
  52.                     } catch (IOException e) {  
  53.                         Log.e(APP_TAG, "ERROR", e);  
  54.                     } finally {  
  55.                         if (fos != null) {  
  56.                             try {  
  57.                                 fos.flush();  
  58.                                 fos.close();  
  59.                             } catch (IOException e) {  
  60.                             }  
  61.                         }  
  62.                     }  
  63.                 } else {  
  64.                     Log.e(APP_TAG, "error writing to file");  
  65.                 }  
  66.   
  67.             } else {  
  68.                 Log.e(APP_TAG, "ERROR, unable to write to /sdcard/test_folder");  
  69.             }  
  70.         } else {  
  71.             Log.e(APP_TAG, "ERROR, /sdcard path not available");  
  72.         }  
  73.   
  74.         // Read file block  
  75.         File readFile = new File(sdDir.getPath() + "/test_folder/" + fileName);  
  76.         if (readFile.exists() && readFile.canRead()) {  
  77.             FileInputStream fis = null;  
  78.             try {  
  79.                 fis = new FileInputStream(readFile);  
  80.                 byte[] reader = new byte[fis.available()];  
  81.                 while (fis.read(reader) != -1) {  
  82.                 }  
  83.                 readOutput.setText(new String(reader));  
  84.             } catch (IOException e) {  
  85.                 Log.e(APP_TAG, e.getMessage(), e);  
  86.             } finally {  
  87.                 if (fis != null) {  
  88.                     try {  
  89.                         fis.close();  
  90.                     } catch (IOException e) {  
  91.                     }  
  92.                 }  
  93.             }  
  94.         } else {  
  95.             readOutput.setText("Unable to read/write sdcard file, see logcat output");  
  96.         }  
  97.     }  
  98. }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值