Android File 数据存储


package com.Aina.Android;

import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;

public class MIDIPlayer {

private Context mContext = null;
private MediaPlayer mMediaPlayer = null;
public MIDIPlayer(Context context){
mContext = context;
}
public void start(){
mMediaPlayer = MediaPlayer.create(mContext, R.raw.start);//装载资源
mMediaPlayer.setLooping(true);//循环播放
try {
mMediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.start();
}

public void stop(){
if(mMediaPlayer != null){
mMediaPlayer.stop();
mMediaPlayer.release();
}
}
}




package com.Aina.Android;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Test_File extends Activity {
/** Called when the activity is first created. */
private MIDIPlayer mMIDIPlayer = null;
private boolean bm = false;
private TextView tv = null;
private Button btn = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMIDIPlayer = new MIDIPlayer(this);
tv = (TextView) this.findViewById(R.id.TextView);
btn = (Button) this.findViewById(R.id.Button01);
this.load();
if (bm) {
mMIDIPlayer.start();
tv.setText("当前音效:开");
} else {
mMIDIPlayer.stop();
tv.setText("当前音效:关");
}
btn.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(Test_File.this, sg.class);
Test_File.this.startActivity(intent);
}

});
}

@Override
protected void onPause() {
this.save();
if (bm) {
mMIDIPlayer.stop();
}
super.onPause();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
mMIDIPlayer.start();
tv.setText("当前音效:开");
bm = true;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
mMIDIPlayer.stop();
tv.setText("当前音效:关");
bm = false;
}
return super.onKeyDown(keyCode, event);
}

// 读取数据
private void load() {
Properties pro = new Properties();
try {
FileInputStream fin = this.openFileInput("music.cfg");// 读取文件
try {
pro.load(fin);// 加载到Properties属性文件里面
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}

bm = Boolean.valueOf(pro.getProperty("isopen", "false"));
}

private String read(Context context, String filepath) {
String data = "";
StringBuffer sb = new StringBuffer();
int ch = 0;
try {
FileInputStream in = context.openFileInput(filepath);
while ((ch = in.read()) != -1) {
sb.append((char) ch);
}
in.close();
in = null;
data = sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}

// 保存数据
private void save() {
Properties pro = new Properties();
pro.put("isopen", String.valueOf(bm));
try {
FileOutputStream fout = this.openFileOutput("music.cfg",
Activity.MODE_WORLD_READABLE
| Activity.MODE_WORLD_WRITEABLE);
pro.store(fout, "");// 写入输出流
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private void write(Context context, String filepath, String msg) {
try {
FileOutputStream out = context.openFileOutput(filepath,
context.MODE_WORLD_WRITEABLE);
out.write(msg.getBytes());
out.flush();
out.close();
out = null;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值