file新建文件及文件夹

1、获取包名的根目录:mRootPath = getFilesDir().getParent();

    // ====mRootPath===/data/data/com.yoyu.file

   获取SD卡的根目录:sdDir = Environment.getExternalStorageDirectory().getAbsolutePath();  

    //====sdDir ====/storage/sdcard0

    系统存储路径  directory = Environment.getRootDirectory().getAbsolutePath();

    //====/system

2、新建一个文件夹为audio的路径:mRootPath = getFilesDir().getParent()+"/audio";

3、创建文件夹:

File file = new File(mRootPath);
// 如果文件夹不存在则创建
if (!file.exists() && !file.isDirectory()) {
    System.out.println("=====//不存在");
    file.mkdirs();
} else {
    System.out.println("====//目录存在");
}

4、在该文件夹下创建一个文件,即需要提供该文件夹下新文件的路径,比如

     path ="/data/data/com.yoyu.audiodemo/audio/1.txt";

  File file = new File(path);

  file.createNewFile();//创建空内容的1.txt

原文地址:https://www.cnblogs.com/kingsam/p/5653469.html