内部存储读写文件

上传者: zsj959800 | 上传时间: 2024-07-12 14:42:20 | 文件大小: 18.67MB | 文件类型: ZIP
在Android开发中,内部存储是应用私有的存储空间,用于保存应用的数据,这些数据对外部不可见,只有应用本身有权限访问。本文将详细介绍如何在Android Studio中获取内部存储路径,创建文件夹,以及进行读写和删除文件的操作。 获取内部存储路径通常使用Android的`getFilesDir()`或`getInternalStorageDirectory()`方法。`getFilesDir()`返回的是应用专属的文件目录,而`getInternalStorageDirectory()`则返回设备的根内部存储路径。例如: ```java File internalStorage = getFilesDir(); // 应用内部存储路径 File systemInternalStorage = Environment.getExternalStorageDirectory(); // 设备内部存储路径 ``` 创建文件夹可以通过`new File(path, folderName)`来实现,其中`path`是父目录路径,`folderName`是文件夹名称。接着调用`mkdir()`或`mkdirs()`确保文件夹创建成功: ```java String folderPath = internalStorage.getAbsolutePath() + "/MyFolder"; File myFolder = new File(folderPath); if (!myFolder.exists()) { myFolder.mkdir(); } ``` 读取文件,可以使用`BufferedReader`或者`InputStreamReader`,以下是一个示例: ```java File file = new File(internalStorage, "example.txt"); if (file.exists()) { BufferedReader reader = new BufferedReader(new FileReader(file)); String line; while ((line = reader.readLine()) != null) { Log.d("Read", line); } reader.close(); } ``` 写入文件,可以使用`BufferedWriter`或`OutputStreamWriter`: ```java try (BufferedWriter writer = new BufferedWriter( new FileWriter(file))) { writer.write("这是写入文件的内容"); } catch (IOException e) { e.printStackTrace(); } ``` 删除文件,使用`delete()`方法: ```java if (file.exists()) { file.delete(); } ``` 对于压缩包子文件的文件名称列表中的"MySd",这可能是表示一个存档文件,例如ZIP或RAR格式。在Android中解压文件,可以使用第三方库如`android-zipfile`或`android-unzip`。以下是一个使用`android-zipfile`库的解压示例: ```java try (ZipFile zipFile = new ZipFile(context.getAssets().openFd("MySd.zip"))) { for ( Enumeration entries = zipFile.entries(); entries.hasMoreElements(); ) { ZipEntry entry = entries.nextElement(); String entryName = entry.getName(); File outputFile = new File(internalStorage, entryName); if (entry.isDirectory()) { outputFile.mkdirs(); } else { InputStream in = zipFile.getInputStream(entry); FileOutputStream out = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); out.close(); } } } catch (IOException e) { e.printStackTrace(); } ``` 以上就是关于Android内部存储读写文件,包括获取路径、创建文件夹、读写操作以及解压文件的基本知识。在实际应用中,还需要考虑异常处理、文件权限管理等更多细节,以确保文件操作的稳定性和安全性。

文件下载

资源详情

[{"title":"( 848 个子文件 18.67MB ) 内部存储读写文件","children":[{"title":"resources-debug.ap_ <span style='color:#111;'> 540.16KB </span>","children":null,"spread":false},{"title":"resources-debug-androidTest.ap_ <span style='color:#111;'> 1.42KB </span>","children":null,"spread":false},{"title":"gradlew.bat <span style='color:#111;'> 2.35KB </span>","children":null,"spread":false},{"title":"fileSnapshots.bin <span style='color:#111;'> 4.77MB </span>","children":null,"spread":false},{"title":"fileHashes.bin <span style='color:#111;'> 533.58KB </span>","children":null,"spread":false},{"title":"taskArtifacts.bin <span style='color:#111;'> 218.46KB </span>","children":null,"spread":false},{"title":"localClassSetAnalysis.bin <span style='color:#111;'> 33.79KB </span>","children":null,"spread":false},{"title":"localClassSetAnalysis.bin <span style='color:#111;'> 24.01KB </span>","children":null,"spread":false},{"title":"localJarClasspathSnapshot.bin <span style='color:#111;'> 21.41KB </span>","children":null,"spread":false},{"title":"localJarClasspathSnapshot.bin <span style='color:#111;'> 19.74KB </span>","children":null,"spread":false},{"title":".gitignore <span style='color:#111;'> 127B </span>","children":null,"spread":false},{"title":".gitignore <span style='color:#111;'> 8B </span>","children":null,"spread":false},{"title":"build.gradle <span style='color:#111;'> 1023B </span>","children":null,"spread":false},{"title":"build.gradle <span style='color:#111;'> 384B </span>","children":null,"spread":false},{"title":"settings.gradle <span style='color:#111;'> 16B </span>","children":null,"spread":false},{"title":"gradlew <span style='color:#111;'> 4.85KB </span>","children":null,"spread":false},{"title":"app.iml <span style='color:#111;'> 10.85KB </span>","children":null,"spread":false},{"title":"main1.iml <span style='color:#111;'> 1.33KB </span>","children":null,"spread":false},{"title":"androidTest4.iml <span style='color:#111;'> 1.06KB </span>","children":null,"spread":false},{"title":"MySd.iml <span style='color:#111;'> 881B </span>","children":null,"spread":false},{"title":"test.iml <span style='color:#111;'> 423B </span>","children":null,"spread":false},{"title":"mockable-android-25.jar <span style='color:#111;'> 32.30MB </span>","children":null,"spread":false},{"title":"gradle-wrapper.jar <span style='color:#111;'> 52.38KB </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 558.30KB </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 107.96KB </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 81.80KB </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 15.08KB </span>","children":null,"spread":false},{"title":"MainActivity.java <span style='color:#111;'> 11.26KB </span>","children":null,"spread":false},{"title":"ExampleInstrumentedTest.java <span style='color:#111;'> 790B </span>","children":null,"spread":false},{"title":"BuildConfig.java <span style='color:#111;'> 477B </span>","children":null,"spread":false},{"title":"BuildConfig.java <span style='color:#111;'> 467B </span>","children":null,"spread":false},{"title":"ExampleUnitTest.java <span style='color:#111;'> 424B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 410B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 362B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 358B </span>","children":null,"spread":false},{"title":"BuildConfig.java <span style='color:#111;'> 272B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 272B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 266B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 263B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 257B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 255B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 254B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 252B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 252B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 250B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 248B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 241B </span>","children":null,"spread":false},{"title":"Manifest.java <span style='color:#111;'> 200B </span>","children":null,"spread":false},{"title":"R.java <span style='color:#111;'> 186B </span>","children":null,"spread":false},{"title":"values.json <span style='color:#111;'> 435.60KB </span>","children":null,"spread":false},{"title":"values-v21.json <span style='color:#111;'> 77.61KB </span>","children":null,"spread":false},{"title":"drawable-xhdpi-v4.json <span style='color:#111;'> 19.06KB </span>","children":null,"spread":false},{"title":"drawable-hdpi-v4.json <span style='color:#111;'> 18.96KB </span>","children":null,"spread":false},{"title":"drawable-mdpi-v4.json <span style='color:#111;'> 18.96KB </span>","children":null,"spread":false},{"title":"values-v11.json <span style='color:#111;'> 18.66KB </span>","children":null,"spread":false},{"title":"drawable-xxhdpi-v4.json <span style='color:#111;'> 17.35KB </span>","children":null,"spread":false},{"title":"values-b+sr+Latn.json <span style='color:#111;'> 15.45KB </span>","children":null,"spread":false},{"title":"values-ne-rNP.json <span style='color:#111;'> 15.33KB </span>","children":null,"spread":false},{"title":"values-bn-rBD.json <span style='color:#111;'> 15.33KB </span>","children":null,"spread":false},{"title":"values-my-rMM.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-et-rEE.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-te-rIN.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-pt-rPT.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-fr-rCA.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-es-rUS.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-pt-rBR.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-ta-rIN.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-kn-rIN.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-ml-rIN.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-mr-rIN.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-gl-rES.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-uz-rUZ.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-bs-rBA.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-be-rBY.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-ka-rGE.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-ms-rMY.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-sq-rAL.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-eu-rES.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-si-rLK.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-ur-rPK.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-kk-rKZ.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-ky-rKG.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-gu-rIN.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-hy-rAM.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-km-rKH.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-mn-rMN.json <span style='color:#111;'> 15.32KB </span>","children":null,"spread":false},{"title":"values-pa-rIN.json <span style='color:#111;'> 15.31KB </span>","children":null,"spread":false},{"title":"values-en-rIN.json <span style='color:#111;'> 15.31KB </span>","children":null,"spread":false},{"title":"values-lo-rLA.json <span style='color:#111;'> 15.31KB </span>","children":null,"spread":false},{"title":"values-en-rGB.json <span style='color:#111;'> 15.31KB </span>","children":null,"spread":false},{"title":"values-en-rAU.json <span style='color:#111;'> 15.31KB </span>","children":null,"spread":false},{"title":"values-is-rIS.json <span style='color:#111;'> 15.31KB </span>","children":null,"spread":false},{"title":"values-zh-rHK.json <span style='color:#111;'> 15.30KB </span>","children":null,"spread":false},{"title":"values-zh-rCN.json <span style='color:#111;'> 15.30KB </span>","children":null,"spread":false},{"title":"values-zh-rTW.json <span style='color:#111;'> 15.30KB </span>","children":null,"spread":false},{"title":"values-tl.json <span style='color:#111;'> 15.15KB </span>","children":null,"spread":false},{"title":"values-hu.json <span style='color:#111;'> 15.15KB </span>","children":null,"spread":false},{"title":"values-pt.json <span style='color:#111;'> 15.15KB </span>","children":null,"spread":false},{"title":"values-fr.json <span style='color:#111;'> 15.15KB </span>","children":null,"spread":false},{"title":"values-ro.json <span style='color:#111;'> 15.15KB </span>","children":null,"spread":false},{"title":"......","children":null,"spread":false},{"title":"<span style='color:steelblue;'>文件过多,未全部展示</span>","children":null,"spread":false}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明