admin管理员组文章数量:1532379
2023年12月13日发(作者:)
android写文件到DOWNLOADS目录
文件加入权限
android 9以上还需要加入requestLegacyExternalStorage
android:requestLegacyExternalStorage="true" ....... 动态权限申请 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == 1000) { if (grantResults[0] == SION_GRANTED) { //同意申请权限 } else { // 用户拒绝申请权限 xt(,"请同意写操作来记录心率原始数据", _SHORT).show(); } return; } estPermissionsResult(requestCode, permissions, grantResults); } onCreate中调用 if (elfPermission(, _EXTERNAL_STORAGE) != PackageManager. PERMISSION_GRANTED) { 1000); } ActivityCompat. requestPermissions( this, new String[]{sion. WRITE_EXTERNAL_STORAGE,_EXTERNAL_STORAGE 文件读写工具类 package ; import ; import edReader; import ; import putStream; import ption; import tream; import treamReader; import AccessFile; import rdCharsets; import s; public class FileUtils { // 将字符串写入到文本文件 public static void writeTxtToFile(String strcontent, String filePath, String fileName) { makeFilePath(filePath, fileName); String strFilePath = filePath +tor+ fileName; String strContent = strcontent + "rn"; try { File file = new File(strFilePath); if (!()) { Log.d("TestFile", "Create the file:" + strFilePath); File parent_file = entFile(); if(parent_file != null){ if(parent_()) { Log.d("TestFile", "parent file path:" + parent_h()); } } if(!NewFile()) { Log.d("TestFile", "文件已经存在,不需要创建"); } } RandomAccessFile raf = new RandomAccessFile(file, "rwd"); (()); (es()); (); } catch (Exception e) { Log.e("TestFile", "Error on write File:" + e); } } //生成文件 public static File makeFilePath(String filePath, String fileName) { File file = null; makeRootDirectory(filePath); try { file = new File(filePath + fileName); if (!()) { if(!NewFile()) { Log.d("TestFile", "文件已经存在,不需要创建"); } } } catch (Exception e) { tackTrace(); } return file; } //生成文件夹 public static void makeRootDirectory(String filePath) { File file = null; try { file = new File(filePath); if (!()) { if(()){ Log.d("TestFile", "file path:" + h()); } } } catch (Exception e) { Log.i("error:", e + ""); } } //读取指定目录下的所有TXT文件的文件内容 public static String getFileContent(File file) { String content = ""; if (!ctory()) { //检查此路径名的文件是否是一个目录(文件夹) if (e().endsWith("txt")) {//文件格式为""文件 try { InputStream instream = new FileInputStream(file); InputStreamReader inputreader = new InputStreamReader(instream, _8); BufferedReader buffreader = new BufferedReader(inputreader); String line = ""; //分行读取 while ((line = ne()) != null) { content += line + "n"; } ();//关闭输入流 } catch (tFoundException e) { Log.d("TestFile", "The File doesn't not exist."); } catch (IOException e) { Log.d("TestFile", eNonNull(sage())); } } } return content; } } 测试写入文件 String temp = "This is a test file"; File file = getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS); xtToFile(temp, h(), "");
版权声明:本文标题:android写文件到DOWNLOADS目录 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1702457661a7937.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论