在我当前的Android应用程序中,我正在使用处理程序从游戏会话中读取数据,然后将其写入文件。
目前,所有数据都写入一个文件。即每个单独的会话数据都在一个文件中。我想改变这一点,以便每次新游戏完成后,它的数据都会输入到自己独立的文件中。
我该怎么办?
当前处理程序:
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// msg.what determines the type of each message
switch (msg.what) {
case TGDevice.MSG_EEG_POWER:
eegPower = (TGEegPower) msg.obj;
//trace code
Log.d("LSD", "highAlpha: " + eegPower.highAlpha);
Log.d("LSD", "lowAlpha: " + eegPower.lowAlpha);
Log.d("LSD", "highBeta: " + eegPower.highBeta);
Log.d("LSD", "lowBeta: " + eegPower.lowBeta);
Log.d("LSD", "lowGamma: " + eegPower.lowGamma);
Log.d("LSD", "midGamma: " + eegPower.midGamma);
Log.d("LSD", "delta: " + eegPower.delta);
Log.d("LSD", "theta: " + eegPower.theta);
//adding all the EEGpowers to an arraylist to help add them to file
ArrayList<String> EEGPowers= new ArrayList<String>();
EEGPowers.add("highAlpha: " + eegPower.highAlpha);
EEGPowers.add("lowAlpha: " + eegPower.lowAlpha);
EEGPowers.add("highBeta: " + eegPower.highBeta);
EEGPowers.add("lowBeta: " + eegPower.lowBeta);
EEGPowers.add("lowGamma: " + eegPower.lowGamma);
EEGPowers.add("midGamma: " + eegPower.midGamma);
EEGPowers.add("delta: " + eegPower.delta);
EEGPowers.add("theta: " + eegPower.theta);
//setting up the array of maxlength etc
spaces[0] = "";
for(int i=1; i<maxWordLength ;i++){
spaces[i] = spaces[i-1]+" ";
}
int seconds=0;
//creating header in txt file
writeToFileEEGPower(order("Seconds")+order("highAlpha")+order("lowAlpha")+order("highBeta")+order("LowBeta")+
order("lowGamma")+order("midGamma")+order("Delta")+order("Theta")+ "\n");
//creating the string to be written to file
String line = order(seconds+"")+order(eegPower.highAlpha+"")+order(eegPower.lowAlpha+"")+order(eegPower.highBeta+"")+
order(eegPower.lowBeta+"")+order(eegPower.midGamma+"")+order(eegPower.delta+"")+order(eegPower.theta+"")+ "\n";
//write the string to file
writeToFileEEGPower(line);
当前的writetofile方法:
public void writeToFileEEGPower(String data){
//creating time for the file
Time t= new Time();
int timeFileSecond= t.second;
int timeFileDate= t.yearDay;
int timeFileYear= t.year;
//creating file name
String fileName= "MathsGame" + timeFileSecond + timeFileDate + timeFileYear + android.os.Build.SERIAL;
//creating the file where the contents will be written to
File file= new File(dir, fileName + ".txt");
FileOutputStream os;
try{
boolean append= true;
os= new FileOutputStream(file, append);
String writeMe =data + "\n";
os.write(writeMe.getBytes());
os.close();
} catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
编辑:
//CURRENTLY CREATES A FILE BASED ON MINUTE, NEED TO CHANGE
Time t= new Time();
t.setToNow();
int timeFileMinute= t.minute;
int timeFileDate= t.yearDay;
int timeFileYear= t.year;
//creating file name
String fileName= timeFileMinute + timeFileDate + timeFileYear + android.os.Build.SERIAL;
//creating the file where the contents will be written to
File file= new File(dir, fileName + ".txt");
答案 0 :(得分:1)
Time t= new Time();
记录将Time对象初始化为1970年1月1日,因此所有文件都是相同的。
要通过尝试执行您所看到的内容,请将对象设置为:
Time t= new Time();
t.setToNow();
timeFileSecond= t.second;
//etc