Android file类使用详解
一.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内核的简单分析
  2. Android学习笔记之开发必备
  3. 索引:Android(安卓)Studio安装及工程项目目录简介
  4. android第一天
  5. android中如何执行java命令
  6. Android(安卓)NDK初识
  7. Android中对Log日志文件的分析
  8. android内存管理
  9. android中如何执行java命令

随机推荐

  1. Android应用实例(一)之---有道辞典VZ.0
  2. Android手游转电视游戏之模拟操控
  3. android中asynctask和thread的区别?
  4. [置顶] android从图库(gallery)选择一张
  5. android代码示例讲解--专题视频课程
  6. Xamarin for android:为button设置click
  7. Android推流帧率的设定与实际情况的影响
  8. 自定义控件(一)
  9. Android NDK rb5 文档之使用 Android 工
  10. android应用安装成功之后删除apk文件