android c 读写文件,在android中读写文件

这篇博客介绍了在Android系统中如何进行文件读写操作,包括使用Java的文件输入流和输出流在内存和SD卡中读写文件。文章详细展示了如何在SD卡上创建和读取文件,并提供了检查SD卡是否挂载以及获取存储路径的方法。同时,强调了在SD卡上写数据需要相应的权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[导读]在android中读写文件android中只有一个盘,正斜杠/代表根目录。我们常见的SDK的位置为:/mnt/sdcard两种最常见的数据存储方式

在android中读写文件

android中只有一个盘,正斜杠/代表根目录。

我们常见的SDK的位置为:/mnt/sdcard

两种最常见的数据存储方式:

一、内存

二、本地

1.手机内部存储

2.外部存储设备(SD卡)

在SD卡中读数据是不需要权限的,但是在SD卡中写数据是要权限的:

读写文件的方式就是用的Java的文件输入流和输出流,和在java中读写文件的方法几乎一模一样。

代码:

com.example.readwrite.MainActivity

6b3580db0e8b74095db80bf86bbd32b1.gif

7e18b6a47d1f0179731b025736c7cff2.gifpackage com.example.readwrite;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

/**

* 正斜杠代表根目录 两种最常见的数据存储方式

*

* 一、内存 二、本地 1.手机内部存储 2.外部存储设备(SD卡)

* */

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// existSDcard();

// write();

//        listPath();

read();

}

private void write() {

// /mnt/sdcard

File file = Environment.getExternalStorageDirectory();

FileOutputStream out = null;

try {

out = new FileOutputStream(file.getPath() + "/bihu.txt");

// out = new FileOutputStream(

// "/data/data/com.example.readwrite/bihu.txt");

out.write("12345".getBytes());

} catch (IOException e) {

e.printStackTrace();

} finally {

if (out != null) {

try {

out.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

private void read() {

FileInputStream in = null;

try {

// in = new FileInputStream("/mnt/sdcard/bihu.txt");

in = new FileInputStream(

"/data/data/com.jiguang.test/databases/rep.db");

byte[] bytes = new byte[2014];

int len = in.read(bytes);

String str = new String(bytes, 0, len);

Log.d("bihu", "---------" + str);

} catch (IOException e) {

Log.d("bihu","报错啦"+e.toString());

} finally {

if (in != null) {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

/**

* 检查SD卡是否被挂载

* */

private void existSDcard() {

// 获取SD卡的状态

String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {

Log.d("bihu", "有SD卡");

} else {

Log.d("bihu", "没有SD卡");

}

}

/**

* 通过API获取路径

* */

private void listPath() {

// 获取SD卡目录

File file1 = Environment.getExternalStorageDirectory();

Log.d("bihu", "sd卡----" + file1.getPath());

// 获取手机内部存储空间的file目录

File file2 = getFilesDir();

Log.d("bihu", "内部存储File----" + file2.getPath());

// 获取内部存储空间的缓存目录

File file3 = getCacheDir();

Log.d("bihu", "内部存储缓存目录----" + file3.getPath());

}

}

主界面

/读写本地文件/AndroidManifest.xml

6b3580db0e8b74095db80bf86bbd32b1.gif

7e18b6a47d1f0179731b025736c7cff2.gif<?xml  version="1.0" encoding="utf-8"?>

package="com.example.readwrite"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="8"

android:targetSdkVersion="19" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name=".MainActivity"

android:label="@string/app_name" >

配置文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值