一.Android file类     在开发Android应用时免不了会跟文件打交道,本篇文章记录总结自己常用到的文件操作,数据的存储有多种方式,比如数据库存储、SharedPreferences存储、文件存储等;这里我们将要介绍最简单的文件存储方式;文件存储简单的来说就是一般的JAVASE中的IO流,只是把他应用于Android手机中而已。

二.Android file类使用

    File文件的存储需要在程序中使用sdcard进行数据的存储,需要在AndroidMainfset.xml文件中进行权限的配置: 1.SDCard中创建与删除文件权限:

2.SDCard写入数据权限
代码如下:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"          package="com.example.cxy.file">    <uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS">uses-permission>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">uses-permission>    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN"/>                <category android:name="android.intent.category.LAUNCHER"/>            intent-filter>        activity>    application>manifest>

    File类语法 1.File(String pathname) File file = new File ("/mnt/sdcard/test.txt");
2.File(String dir, String subpath) File file = new File("/mnt/sdcard/temp", "test.txt");
          File类常用方法
boolean exists() 测试文件是否存在
boolean delete() 删除此对象指定的文件
boolean createNewFile() 创建新的空文件
boolean isDirectory() 测试此File对象表示的文件是否是目录
boolean mkdir() 创建由该File对象表示的目录
boolean mkdirs() 创建包括父目录的目录
String getAbsolutePath() 返回此对象表示的文件的绝对路径名
String getName() 返回此对象表示的文件的名称
String getParent() 返回此File对象的路径名的上一级,若路径名没有上一级,则返回null

使用mkdir创建由该File对象表示的目录
package com.example.cxy.file;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.Toast;import java.io.File;import java.io.IOException;public class MainActivity extends AppCompatActivity implements View.OnClickListener{    private Button btn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView(){        btn= (Button) findViewById(R.id.button);        btn.setOnClickListener(this);    }    @Override    public void onClick(View v) {        //先实例化一个file对象,参数为路径名        File file = new File("/mnt/sdcard/Tenect/chenxiaoyang.txt");        //File file = new File("/mnt/sdcard/Tenect","chenxiaoyang.txt");        try {            //判断文件是否存在            if (file.exists()){                //文件如果存在删除这个文件                file.delete();                Toast.makeText(MainActivity.this, "删除成功了", Toast.LENGTH_SHORT).show();            }else{                //创建一个新的文件                file=new File("/mnt/sdcard/Tenect");                //先创建文件夹                file.mkdir();                //创建这个文件                file = new File("/mnt/sdcard/Tenect/chenxiaoyang.txt");                file.createNewFile();                Toast.makeText(MainActivity.this, "创建成功了", Toast.LENGTH_SHORT).show();            }            //获取当前file文件的绝对路径            Log.i("$$$",file.getAbsolutePath());            //获取当前file文件的名字,包括后缀名            Log.i("$$$",file.getName());            //获取当前file文件的以上所有级的目录            Log.i("$$$",file.getParent());            //测试此file文件是否是一个目录            boolean directory=file.isDirectory();            Log.i("$$$",String.valueOf(directory));        } catch (IOException e) {            e.printStackTrace();            Toast.makeText(MainActivity.this, "失败了", Toast.LENGTH_SHORT).show();        }    }}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.cxy.file.MainActivity">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="写入"        android:id="@+id/button"        android:layout_centerVertical="true"        android:layout_centerHorizontal="true"/>RelativeLayout>







使用mkdirs创建包括父目录的目录
package com.example.cxy.myapplication;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;import java.io.File;import java.io.IOException;public class MainActivity extends AppCompatActivity implements View.OnClickListener{    private Button btn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        inintView();    }    private void inintView() {        btn= (Button) findViewById(R.id.button);        btn.setOnClickListener(this);    }    @Override    public void onClick(View v) {        //先实例化一个file对象,参数为路径名        File file=new File("/mnt/sdcard/tmp/one/two/three","test.txt");        try {            //判断文件是否存在            if(file.exists()){                //文件如果存在删除这个文件                file.delete();                Toast.makeText(MainActivity.this,"删除成功", Toast.LENGTH_SHORT).show();            }else{                //创建一个新的文件                file=new File("/mnt/sdcard/tmp/one/tow/three");                //先创建文件夹,mkdirds可直接创建多级文件夹                file.mkdirs();                //创建这个文件                file=new File("/mnt/sdcard/tmp/one/tow/three/test.txt");                file.createNewFile();            }        } catch (IOException e) {            e.printStackTrace();        }    }}

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.cxy.myapplication.MainActivity">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="写入"        android:id="@+id/button"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="56dp"/>RelativeLayout>

更多相关文章

  1. Android中的SVG资源
  2. Android(安卓)camera2使用
  3. Android(安卓)核心分析 之六 -----IPC框架分析 Binder,Service,Se.
  4. Android(安卓)NDK环境搭建和开发入门
  5. android设置对话框背景透明度和弹出位置
  6. Android系统给第三方app签名流程
  7. Android开发主页框架搭建
  8. Android中ActivityManagerService与应用程序(客户端)通信模型分
  9. Android创建和使用数据库SQLIte

随机推荐

  1. Android(安卓)短信列表的时间显示
  2. Spring Boot使用MongoDB
  3. JumpServer部署使用
  4. 滴滴:等干完这一票,我就不干了
  5. CentOS7安装MongoDB4
  6. 最短路径之狄克斯特拉(Dijkstra)算法
  7. 深圳又一程序员倒下,内心感到悲凉!
  8. 最短路径之贝尔曼-福特算法
  9. 谈谈对Linux的Huge Pages与Transparent H
  10. 华为外包程序员跳楼,难道这是我的35岁?