版權聲明:本文爲HaiyuKing原創文章,轉載請註明出處!html
封裝了獲取SD卡根目錄路徑、以及對文件讀寫、獲取文件名等相關操做。java
由於須要用到android.permission.READ_EXTERNAL_STORAGE權限,因此依賴《Android6.0運行時權限(基於RxPermission開源庫)》。android
較經常使用的應該是獲取SD卡根目錄路徑、獲取文件名、建立目錄操做。git
注意事項:github
一、 導入類文件後須要change包名以及從新import R文件路徑apache
二、 Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),若是項目中存在,則複製裏面的內容,不要整個覆蓋app
package com.why.project.fileutilsdemo; import android.app.Application; import android.content.Context; /** * Created by HaiyuKing * Used 自定義Application【系統上下文】 */ public class MyApplication extends Application { /**系統上下文*/ private static Context mAppContext; @Override public void onCreate() { super.onCreate(); mAppContext = getApplicationContext(); } /**獲取系統上下文:用於FileUtils工具類、Utility類*/ public static Context getAppContext() { return mAppContext; } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.why.project.fileutilsdemo"> <!-- ======================(FileUtil)========================== --> <!-- 向SD卡寫入數據權限 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" android:name=".MyApplication"> <activity android:name=".activity.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.why.project.fileutilsdemo"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
//FileUtils compile 'org.apache.httpcomponents:httpcore:4.4.4'
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
}
對於Eclipse開發環境,在libs目錄下導入httpcore-4.4.4.jar包便可。eclipse
連接:http://pan.baidu.com/s/1pKUzNiN 密碼:xa8cide
private void initEvents() { //獲取SD卡路徑 tv_getSdPath.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String sdPath = FileUtils.getSDPath(); Log.w(TAG,"sdPath="+sdPath); } }); //建立目錄 tv_makeFolders.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String fileForderPath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator; Log.w(TAG,"fileForderPath="+fileForderPath); boolean makeFolderState = FileUtils.makeFolders(fileForderPath); Log.w(TAG,"makeFolderState="+makeFolderState); } }); //將字符串寫入文件 tv_writeFile.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String filePath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator + "file.txt"; Log.w(TAG,"filePath="+filePath); String content = "將字符串內容添加到文本文件中"; boolean writeFileState = FileUtils.writeFile(filePath,content); Log.w(TAG,"writeFileState="+writeFileState); } }); //讀取文件 tv_readFile.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String filePath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator + "file.txt"; Log.w(TAG,"filePath="+filePath); String readContent = FileUtils.readFile(filePath); Log.w(TAG,"readContent="+readContent); } }); //獲取文件名(不帶後綴) tv_getFileNameWithoutExtension.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String filePath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator + "file.txt"; Log.w(TAG,"filePath="+filePath); String fileNameWithoutExtension = FileUtils.getFileNameWithoutExtension(filePath); Log.w(TAG,"fileNameWithoutExtension="+fileNameWithoutExtension); } }); //獲取文件名(帶後綴) tv_getFileName.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String filePath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator + "file.txt"; Log.w(TAG,"filePath="+filePath); String fileName = FileUtils.getFileName(filePath); Log.w(TAG,"fileName="+fileName); } }); //獲取後綴名 tv_getFileExtension.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String filePath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator + "file.txt"; Log.w(TAG,"filePath="+filePath); String fileExtension = FileUtils.getFileExtension(filePath); Log.w(TAG,"fileExtension="+fileExtension); } }); //獲取文件大小 tv_getFileSize.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String filePath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator + "file.txt"; Log.w(TAG,"filePath="+filePath); long fileSize = FileUtils.getFileSize(filePath); Log.w(TAG,"fileSize="+fileSize); } }); //刪除文件 tv_deleteFileRecursion.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String filePath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator + "file.txt"; Log.w(TAG,"filePath="+filePath); FileUtils.deleteFileRecursion(filePath); } }); //獲取APP的文件路徑 tv_getAppFilePath.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String appFilePath = FileUtils.getAppFilePath(); Log.w(TAG,"appFilePath="+appFilePath); } }); }
#=====================httpcore=====================
#android Studio環境中不須要,eclipse環境中須要
#-libraryjars libs/httpcore-4.4.4.jar
-dontwarn org.apache.http.**
-keep class org.apache.http.**{*;}