第九篇-新建文件夹和文本文件mkdirs,createNewFile
一、新建一个empty activity的项目
二、修改AndroidMainfest.xml,添加用户权限。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aimee.test1">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
三、修改activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="CreateFile"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
四、修改MainActivity.java
package com.example.aimee.test1; import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast; import java.io.File;
import java.io.IOException; public class MainActivity extends AppCompatActivity {
private final int REQUESTCODE=101;
private String fileName="test"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
create(fileName);
} catch (IOException e) {
e.printStackTrace();
} }
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
if(requestCode==REQUESTCODE){
if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE) && grantResults[0]== PackageManager.PERMISSION_GRANTED){
Toast.makeText(MainActivity.this,"111",Toast.LENGTH_LONG).show(); }else {
Toast.makeText(MainActivity.this,"222",Toast.LENGTH_LONG).show();
}
}
} public void create(String fileName) throws IOException {
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
int checkSelfPermission=checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(checkSelfPermission==PackageManager.PERMISSION_DENIED){
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUESTCODE);
}
}
String path=Environment.getExternalStorageDirectory().getAbsolutePath();
File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+fileName);
if(!file.exists()){
boolean isSuccess=file.mkdirs();
Toast.makeText(MainActivity.this,"333",Toast.LENGTH_LONG).show(); }else{
String file_name=path+"/test.txt";
File f=new File(file_name);
if(!f.exists()){
Toast.makeText(MainActivity.this,"文件不存在",Toast.LENGTH_LONG).show();
f.createNewFile();
}
else{
Toast.makeText(MainActivity.this,"文件创建成功",Toast.LENGTH_LONG).show();
}
Toast.makeText(MainActivity.this,path,Toast.LENGTH_LONG).show();
}
}
}
如此便可以创建一个文件夹test,在手机的根目录下,同时创建一个text.txt的文件,在test文件夹下。
第九篇-新建文件夹和文本文件mkdirs,createNewFile的更多相关文章
- [实战]MVC5+EF6+MySql企业网盘实战(12)——新建文件夹和上传文件
写在前面 之前的上传文件的功能,只能上传到根目录,前两篇文章实现了新建文件夹的功能,则这里对上传文件的功能进行适配. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战] ...
- [实战]MVC5+EF6+MySql企业网盘实战(10)——新建文件夹
写在前面 上篇文章更新了编辑了文件名的操作,本片文章将实现新建文件夹的功能. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘实战 ...
- Mac新建文件夹、txt文件、无格式文件
新建文件夹: mkdir test 新建txt touch test.txt 新建无后缀格式文件 touch test 如果要删除文件夹 rm -r -f test
- win10没有新建文件夹
win10没有新建文件夹 win10系统,电脑点击右键没有新建文件夹选项. 工具/原料 win10 系统台式机 方法/步骤 电脑左下角搜素出输入:cmd 出现以下画面 在命令指示符 ...
- Delphi 弹出Windows风格的选择文件夹对话框, 还可以新建文件夹
Delphi 弹出Windows风格的选择文件夹对话框, 还可以新建文件夹 unit Unit2; interface uses Windows, Messages, SysUtils, V ...
- win10无法新建文件夹怎么办 win10右键新建菜单设置方法
有朋友安装了win10系统后发现右键菜单中没有新建项,而平时使用新建 - 文件夹项的机率很高.如何才能恢复桌面右键菜单中的新建项呢? 右键点击桌面空白处,在右键菜单中发现没有新建项: 桌面右键菜单没有 ...
- 每天一个JS 小demo之新建文件夹。主要知识点:DOM方法的综合运用
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"& ...
- 解决 win10 新建文件夹重命名卡死的另一种方法
遇到 win10 新建文件夹重命名卡死时 根据网上的各种方法都不起作用时可以试一下这个方法. 文件夹属性 --- 自定义 --- 你想要那种文件夹 优化此文件夹 --- 把 [视频] 改成 [常规项目 ...
- 在res文件下新建文件夹
今天遇到了在res下新建文件夹的问题,无论是是Android studio中直接建还是在下载Android studio的物理地址中直接新建一个文件夹,在树结构中始终没有见到新建的文件,原来需要把An ...
随机推荐
- python之路--JavaScript
一. JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,希望这门语言能成为国际化标准,于是决定将Jav ...
- python之路--MySQl单表查询
一. 关键字的执行优先级(重点) from where group by having # 使用是要放在group by 后面而且前面必须有group by select distinct # 去重 ...
- vscode git设置
vscode只能打开一下界面: 在setting.path增加git.path选项,再使用linux的方法配置路径,就是使用D:/../bin/git.exe而不是\\ 重启vscode,git设置即 ...
- cookie中的小错误
今天在练习 cookie时意外的报了这个错. 这句话的意思是一个不识别的字符[32]出现在了cookie当中由于tomcat的版本比较高,所以在addCookie时是不能使用空格的 而在ASCII码中 ...
- hdu-5687(字典树)
题意:中文题: 解题思路:增加和查询就不说了,标准操作,就是删除操作:删除操作的时候,我们把给定字符串先在字典树中遍历一遍,然后算出这个字符串最后一个字符的出现次数,然后在遍历一遍,每个节点都减去这个 ...
- poj2112(网络流-最大流+二分)
题意:给你k个挤奶器,c头牛,每个挤奶器能放m头牛,问你奶牛需要走的最大距离最小是多少: 解题思路:因为最大距离最小,也就是求最小的距离满足所有牛都能到,所以我们先用floyd跑最短路,把所有点之间的 ...
- NAND闪存供过于求的情况今年会有所好转吗?
2018年,NAND闪存全年供过于求,价格一直下跌,导致西数.东芝等厂商毛利率大幅下滑.如今到了2019年,情况会有所好转吗? 近日,集邦科技旗下半导体研究中心DRAMeXchange发布调查报告指出 ...
- python---反射详解
反射即想到4个内置函数分别为:getattr.hasattr.setattr.delattr 获取成员.检查成员.设置成员.删除成员 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- openwrt-scripts/config/mconf: Syntax error: “(” unexpected错误解决
scripts/config/mconf: Syntax error: “(” unexpected错误解决 从其他地方复制而来的openwrt SDK,放在本地执行make menuconfig时出 ...
- linux-shell系列5-统计
#!/bin/bashshow=$(service --status-all 2>/dev/null | grep -E "is running|正在运行"|awk '{pr ...