Android文件处理

下面的代码是表示存储信息与读取信息。

	public static boolean saveInfo(String text, String text2) {
			String msg=text+"#"+text2;
			File file=new File("data/data/com.example.day03_pratice/info.txt");
			try {
				FileOutputStream fos=new FileOutputStream(file);
				fos.write(msg.getBytes());
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			} 
			
			return true;
	}
	public static String readInfo() {
		File file=new File("data/data/com.example.day03_pratice/info.txt");
		try {
			BufferedReader br=new BufferedReader(new FileReader(file));
			return br.readLine();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
之所以放在文件data/data/com.example.day03_pratice中,是因为这个文件夹是私有的文件夹,其它项目一般无法访问此项目,可以保证数据的隐密性。

使用Context获得常见目录,而不是把目录写死(容易出问题),可以理解为大工具类,可以从这里获得全局信息(系统资源),也可以访问当前私有资源和类,也可以做系统级的调用,比如开启另外一个activity 发广播  开启服务。

File getFilesDir(); 获得的目录是data/data/包名/files/      目录对应的文件

FileOutputStream openFileOutput("info.txt",  );    可以获得输出流 ,操作getFilesDir()对应目录下的文件。

FileInputStream openFileInput("info.txt", );  可以获得输入流 ,操作getFilesDir()对应目录下的文件。

注意:context.getFilesDir().getAbsolutePath()  后面若要跟文件则要加“/”

而若是直接写context.openFilesDir("info2.txt" ,   ...) ;  这里的文件不可以加“/”

	public static boolean saveInfo(Context context,String text, String text2) {
			String msg=text+"#"+text2;
			File file=new File(context.getFilesDir().getAbsolutePath()+"/info2.txt");
			try {
				FileOutputStream fos=context.openFileOutput("info2.txt", Context.MODE_WORLD_WRITEABLE);
				fos.write(msg.getBytes());
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			} 
			
			return true;
	}
	public static String readInfo(Context context) {
		File file=new File(context.getFilesDir().getAbsolutePath()+"/info2.txt");
		try {
			BufferedReader br=new BufferedReader(new FileReader(file));
			return br.readLine();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
若是想把信息储存在SD卡上,则需要获取权限,在 AndroidManifest.xml 中,在application标签外加上
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

	public static boolean saveInfo(String text, String text2) {
			String msg=text+"#"+text2;
			try {
				FileOutputStream fos=new FileOutputStream("mnt/sdcard/info2.txt");
				fos.write(msg.getBytes());
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			} 
			return true;
	}
	public static String readInfo() {
		File file=new File("mnt/sdcard/info2.txt");
		try {
			BufferedReader br=new BufferedReader(new FileReader(file));
			return br.readLine();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

sdcard的位置很多情况下都不固定,直接写死路径也是万万不行的,可以先获得当前项目外部存储设备的路径。

	private static String sdcard=Environment.getExternalStorageDirectory().getAbsolutePath();
	public static boolean saveInfo(String text, String text2) {
			String msg=text+"#"+text2;
			try {
				FileOutputStream fos=new FileOutputStream(sdcard+"/info2.txt");
				fos.write(msg.getBytes());
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			} 
			return true;
	}
	public static String readInfo() {
		File file=new File(sdcard+"/info2.txt");
		try {
			BufferedReader br=new BufferedReader(new FileReader(file));
			return br.readLine();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

判断是否有sd卡插入,若有则在sd卡上保存数据,若无,则在files中存储数据。

Environment.getExternalStorageState()  等于 Environment.MEDIA_MOUNTED则插了sd卡,否则不存在sd卡。

/*
 * 将一个String文件化
 */
public class Utils {
	private static boolean storeInSd=false;//标记是否存入sd卡
	private static String sdcard=Environment.getExternalStorageDirectory().getAbsolutePath();//获取sd卡的路径
	public static boolean saveInfo(Context context,String text, String text2) {
			String msg=text+"#"+text2;
			try {
				if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
					storeInSd=true;
				FileOutputStream fos;
				if(storeInSd)
				{
					 fos=new FileOutputStream(sdcard+"/info2.txt");
				}
				else{
					fos=context.openFileOutput("info2.txt", Context.MODE_WORLD_WRITEABLE);
				}
				fos.write(msg.getBytes());
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			} 
			return true;
	}
	public static String readInfo(Context context) {
		File file;
		if(storeInSd)
		{
			file=new File(sdcard+"/info2.txt");
		}
		else{
			file=new File(context.getFilesDir().getAbsolutePath()+"/info2.txt");
		}
		try {
			BufferedReader br=new BufferedReader(new FileReader(file));
			return br.readLine();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

一共十位,第一位表示是否为目录, 接下来三位表示当前用户是否有权限(r,w,x), 同组用户是否有权限,其它用户是否有权限。

r (读)   w(写)    x(execute 可执行)

当前用户表示同一个项目中的其它方法,其它用户表示其它项目中的方法访问此文件。


修改文件权限:

adb shell

cd 切换到待修改文件所在路径

chmod命令可以修改权限 每三位合成一个数 比如chmod 777 info2.txt   便是把所有权限都赋给info2.txt


context.openFileOutput("file.txt", mode);

mode:1.PRIVATE(660)  2.WORLD_READABLE(664)  3.WORLD_WRITEABLE(662) 4.APPEND(660)

也就是说,WORLD_READABLE其它用户也可以读取此文件,WORLD_WRITEABLE其它用户也可以写这个文件,所以过时了。

APPEND在文件后追加,PRIVATE 再次写会覆盖掉,只有同组访问。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中处理WAV文件,可以使用Android的MediaRecorder和MediaPlayer类。以下是一个简单的示例代码,演示如何录制和播放WAV文件: 录制WAV文件: ```java private MediaRecorder mediaRecorder; private String outputFile; private void startRecording() { outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.wav"; mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mediaRecorder.setOutputFile(outputFile); try { mediaRecorder.prepare(); mediaRecorder.start(); } catch (IOException e) { e.printStackTrace(); } } private void stopRecording() { if (mediaRecorder != null) { mediaRecorder.stop(); mediaRecorder.release(); mediaRecorder = null; } } ``` 播放WAV文件: ```java private MediaPlayer mediaPlayer; private void playRecording() { mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(outputFile); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); } } private void stopPlayback() { if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null; } } ``` 记得添加相应的权限到AndroidManifest.xml文件中: ```xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值