zoukankan      html  css  js  c++  java
  • 在android中读写文件

    在android中读写文件

    android中只有一个盘,正斜杠/代表根目录。

    我们常见的SDK的位置为:/mnt/sdcard

    两种最常见的数据存储方式:

    一、内存

    二、本地

      1.手机内部存储

      2.外部存储设备(SD卡)

    在SD卡中读数据是不需要权限的,但是在SD卡中写数据是要权限的:

      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    读写文件的方式就是用的Java的文件输入流和输出流,和在java中读写文件的方法几乎一模一样。

    代码:

    com.example.readwrite.MainActivity

      1 package com.example.readwrite;
      2 
      3 import java.io.File;
      4 import java.io.FileInputStream;
      5 import java.io.FileOutputStream;
      6 import java.io.IOException;
      7 
      8 import android.app.Activity;
      9 import android.os.Bundle;
     10 import android.os.Environment;
     11 import android.util.Log;
     12 
     13 /**
     14  * 正斜杠代表根目录 两种最常见的数据存储方式
     15  * 
     16  * 一、内存 二、本地 1.手机内部存储 2.外部存储设备(SD卡)
     17  * */
     18 public class MainActivity extends Activity {
     19 
     20     @Override
     21     protected void onCreate(Bundle savedInstanceState) {
     22         super.onCreate(savedInstanceState);
     23         setContentView(R.layout.activity_main);
     24         // existSDcard();
     25         // write();
     26 //        listPath();
     27         read();
     28     }
     29 
     30     private void write() {
     31         // /mnt/sdcard
     32         File file = Environment.getExternalStorageDirectory();
     33         FileOutputStream out = null;
     34         try {
     35             out = new FileOutputStream(file.getPath() + "/bihu.txt");
     36             // out = new FileOutputStream(
     37             // "/data/data/com.example.readwrite/bihu.txt");
     38             out.write("12345".getBytes());
     39         } catch (IOException e) {
     40             e.printStackTrace();
     41         } finally {
     42             if (out != null) {
     43                 try {
     44                     out.close();
     45                 } catch (IOException e) {
     46                     // TODO Auto-generated catch block
     47                     e.printStackTrace();
     48                 }
     49             }
     50         }
     51     }
     52 
     53     private void read() {
     54         FileInputStream in = null;
     55         try {
     56             // in = new FileInputStream("/mnt/sdcard/bihu.txt");
     57             in = new FileInputStream(
     58                     "/data/data/com.jiguang.test/databases/rep.db");
     59             byte[] bytes = new byte[2014];
     60             int len = in.read(bytes);
     61             String str = new String(bytes, 0, len);
     62             Log.d("bihu", "---------" + str);
     63         } catch (IOException e) {
     64             Log.d("bihu","报错啦"+e.toString());
     65         } finally {
     66             if (in != null) {
     67                 try {
     68                     in.close();
     69                 } catch (IOException e) {
     70                     e.printStackTrace();
     71                 }
     72             }
     73         }
     74     }
     75 
     76     /**
     77      * 检查SD卡是否被挂载
     78      * */
     79     private void existSDcard() {
     80         // 获取SD卡的状态
     81         String state = Environment.getExternalStorageState();
     82 
     83         if (Environment.MEDIA_MOUNTED.equals(state)) {
     84             Log.d("bihu", "有SD卡");
     85         } else {
     86             Log.d("bihu", "没有SD卡");
     87         }
     88     }
     89 
     90     /**
     91      * 通过API获取路径
     92      * */
     93     private void listPath() {
     94         // 获取SD卡目录
     95         File file1 = Environment.getExternalStorageDirectory();
     96         Log.d("bihu", "sd卡----" + file1.getPath());
     97         // 获取手机内部存储空间的file目录
     98         File file2 = getFilesDir();
     99         Log.d("bihu", "内部存储File----" + file2.getPath());
    100         // 获取内部存储空间的缓存目录
    101         File file3 = getCacheDir();
    102         Log.d("bihu", "内部存储缓存目录----" + file3.getPath());
    103     }
    104 }
    主界面

    /读写本地文件/AndroidManifest.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.example.readwrite"
     4     android:versionCode="1"
     5     android:versionName="1.0" >
     6     
     7     <!-- 在外部设备(SD卡)上写入的权限 -->
     8     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     9 
    10     <uses-sdk
    11         android:minSdkVersion="8"
    12         android:targetSdkVersion="19" />
    13 
    14     <application
    15         android:allowBackup="true"
    16         android:icon="@drawable/ic_launcher"
    17         android:label="@string/app_name"
    18         android:theme="@style/AppTheme" >
    19         <activity
    20             android:name=".MainActivity"
    21             android:label="@string/app_name" >
    22             <intent-filter>
    23                 <action android:name="android.intent.action.MAIN" />
    24 
    25                 <category android:name="android.intent.category.LAUNCHER" />
    26             </intent-filter>
    27         </activity>
    28     </application>
    29 
    30 </manifest>
    配置文件
  • 相关阅读:
    selenium+python自动化86-Chrome正在受到自动软件的控制
    python笔记6-%u60A0和u60a0类似unicode解码
    百度网页搜索部
    百度:替换和清除空格
    百度:在O(1)空间复杂度范围内对一个数组中前后连段有序数组进行归并排序
    WLLCM这五个字母全排列数目
    r个有标志的球放进n个不同的盒子里,要求无一空盒,问有多少种不同的分配方案?
    从某一日期开始过day天的日期
    求从1到500的整数中能被3和5整除但不能被7整除的数的个数
    红、黄、蓝三色的球各8个,从中取出9个,要求每种颜色的球至少一个,问有多少种不同的取法?
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/7438504.html
Copyright © 2011-2022 走看看