我正在将一个双重列表(mydata)保存到一个文件中,它是用户输入的一些数据和一个字符串列表(dates_Strings),它是当前日期。
用户输入一些数据并按下“保存”按钮,我保存数据和当前日期。
因此,用户可以输入“1”并按保存 (1, 08/05/13)
输入“2”并按保存 (2, 08/05/13)。
因为用户可能在一天内(同一日期)输入数据,所以我不想保存许多日期实例。我想保存该日期的所有用户数据。
我试过这样的:
for (int i=1;i<mydata.size();i++){
bw.write(mydata.get(i)+",");
while (!(dates_Strings.get(i).equals(dates_Strings.get(i-1))))
bw.write(dates_Strings.get(i)+"\n");
}
但它只保存最后输入的数据。
我保存为:
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard, "MyFiles");
directory.mkdirs();
File file = new File(directory, filename);
FileOutputStream fos;
//saving them
try {
fos = new FileOutputStream(file,true); //true in order to append
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (int i=1;i<mydata.size();i++){
//if (!(dates_Strings.get(i).equals(dates_Strings.get(i-1))))
bw.write(mydata.get(i)+","+dates_Strings.get(i)+"\n");
}
value.setText("");
bw.flush();
bw.close();
} catch (IOException e2) {
e2.printStackTrace();
}//catch
}
我正在加载:
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard, "MyFiles");
File file = new File(directory, filename);
String s;
FileInputStream fis;
try {
fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
do {
s = br.readLine();
if (s != null ){
String[] splitLine = s.split(",");
mydata.add(Double.parseDouble(splitLine[0]));
//dates_Strings.add(thedate.parse(splitLine[1]));
dates_Strings.add(splitLine[1]);
}
} while (s != null );
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}