这篇文章主要为大家详细介绍了Android读写文件工具类,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Android读写文件工具类的具体代码,供大家参考,具体内容如下
public class Utils {
private static String path1 = Environment.getExternalStorageDirectory().getAbsolutePath();
private static String path2 = Environment.getDownloadCacheDirectory().getAbsolutePath();
private static String pathExt = "/111/222/333/444/555/";
private static String fileName = "6.txt";
public static void write(String str) {
String filePath = null;
boolean hasSDCard =Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if (hasSDCard) {
filePath = path1 + pathExt + fileName;
} else {
filePath = path2 + pathExt + fileName;
}
try {
File file = new File(filePath);
if (!file.exists()) {
//mkdirs()方法生成多层文件夹
//mkdir()方法生成一层层文件夹
// File dir = new File(file.getParent());
// dir.mkdirs();
file.getParentFile().mkdirs();//生成文件外层的文件夹
file.createNewFile();//生成文件
}
FileOutputStream os = new FileOutputStream(file);
os.write(str.getBytes());
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String read() {
String content = "";
String filePath;
boolean sdcard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if (sdcard) {
filePath = path1 + pathExt + fileName;
} else {
filePath = path2 + pathExt + fileName;
}
try {
File file = new File(filePath);
if (file.exists()) {
FileInputStream is = new FileInputStream(file);
InputStreamReader inputReader = new InputStreamReader(is);//设置流读取方式
BufferedReader buffReader = new BufferedReader(inputReader);
String line;
try {
while (null != (line = buffReader.readLine())) {
content += line + "\n";//读取的文件容
}
is.close();//关闭输入流
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return content;
}
}
1. 清单文件中添加读写权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2.Android6.0以上版本要动态申请读写权限
ArrayList<String> permissionList = new ArrayList<>();
private String[] permissions = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE" };
//检测是否有写的权限
//判断手机版本,如果低于6.0 则不用申请权限,直接拍照
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(permissions[0]);
}
if (checkSelfPermission(permissions[1]) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(permissions[1]);
}
if (!permissionList.isEmpty()) {
String[] permissions1 = permissionList.toArray(new String[permissionList.size()]);
requestPermissions(permissions1, 1);
} else {
Utils.write("balabala");
Utils.read();
}
} else {
Utils.write("balabala");
Utils.read();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 1:
if (PackageManager.PERMISSION_GRANTED == grantResults[0]){
Utils.write("balabala");
Utils.read();
} else {
Log.d(TAG, "fail: ");
}
break;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:Android读写文件工具类详解
基础教程推荐
猜你喜欢
- Android开发Compose集成高德地图实例 2023-06-15
- IOS获取系统相册中照片的示例代码 2023-01-03
- Android实现短信验证码输入框 2023-04-29
- iOS开发 全机型适配解决方法 2023-01-14
- iOS中如何判断当前网络环境是2G/3G/4G/5G/WiFi 2023-06-18
- Android Compose自定义TextField实现自定义的输入框 2023-05-13
- MVVMLight项目Model View结构及全局视图模型注入器 2023-05-07
- iOS开发使用XML解析网络数据 2022-11-12
- Flutter进阶之实现动画效果(三) 2022-10-28
- iOS Crash常规跟踪方法及Bugly集成运用详细介绍 2023-01-18