/** * * @param context 刷新文件使用需要(否则电脑上不能第一时间获取到路径下的文件,重启之后刷新) * @param file 读写的文件 * @param stringList 写入的数据 */ private void writeTxt(Context context, File file, List<String> stringList) { try { if (!file.exists()) { file.createNewFile();//文件不存在时,创建文件 } RandomAccessFile raf = new RandomAccessFile(file, "rwd");//随机流(RandomAccessFile)不属于IO流,支持对文件的读取和写入随机访问。 raf.seek(file.length());//续写文件 for (String content : stringList) { String Content = content + "\r\n"; raf.write(Content.getBytes()); } raf.close(); //刷新文件 MediaScannerConnection.scanFile(context, new String[]{file.getPath()}, null, null); Log.d(TAG, "导出成功"); } catch (Exception e) { e.printStackTrace(); Log.d(TAG, "导出异常"); } }
Android写入txt文件
Android文件导出
最新推荐文章于 2024-04-28 07:30:38 发布
本文介绍了一种在Android应用中实现文件导出的方法。通过使用RandomAccessFile进行文件的读写操作,并利用MediaScannerConnection刷新文件,确保文件可以被外部应用及时访问。
864

被折叠的 条评论
为什么被折叠?



