我目前正在将值保存到应用程序中的文本文件中。每秒从EEG耳机读取值,然后将其存储在文本文件中。
使用处理程序读取值,例如:
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);
for(String s: EEGPowers){
writeToFileEEGPower(s);
}
//rest of handler...
以下方法是用于将值保存到文件的方法:
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();
}
}
方法和处理程序按原样工作,但是我的问题是这些值在保存时没有按照我想要的格式进行格式化,并且非常混乱,如您所见:
E.g。当前文本文件:
我希望我的文本文件格式为,如下所示:
如何在我的代码中实现此格式?
编辑(尝试格式化):
//Declared globally outside handler
final int maxWordLength = 15;
String spaces[] = new String[maxWordLength];
//在处理程序中:
//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 THE HEADER IN THE TEXT 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);
文本文件中的当前样本输出:
答案 0 :(得分:1)
您可以定义最大'单词'长度,并用空格填写每个'单词'。
例如:
定义maxWordLength。此值描述每列的宽度。该值是否应略大于最长的单词(在此示例中,'highAlpha:'= 10的长度,请选择数字&gt; 10)。
final int maxWordLength = 15;
创建空格数组。
String spaces[] = {""," "," "," "," ", /*....*/ " "};// Complete the missing words up to the last word with 15 spaces.
或动态创建:
String spaces[] = new String[maxWordLength];
spaces[0] = "";
for(int i=1; i<maxWordLength ;i++){
spaces[i] = spaces[i-1]+" ";
}
定义'Seconds'var,在类中创建order函数并将标题行写入文件:
int seconds=0;
//表头行 writeToFileEEGPower(顺序( “秒”)+的顺序( “highAlpha”)+的顺序( “lowAlpha”)+的顺序( “highBeta”)/ + ... /);
private String order(String value){
return (value + spaces[maxWordLength-value.length()]);
}
对于每个handleMessage,创建一行并将其保存到文件中:
String line = order(seconds+"")+order(eegPower.highAlpha+"")+order(eegPower.lowAlpha+"")+order(eegPower.highBeta+"")//+...;
writeToFileEEGPower(line);