0

我正在将一个双重列表(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();
           }
}
4

2 回答 2

0

您必须在文件中加载以前的值.. 读取并添加新值.. 然后保存!

于 2013-05-08T16:00:16.157 回答
0

嗯......也许这可以帮助你,我们同事提到的基本思想:接收输入,将其保存在文件中,接收新输入,之前读取现有文件,将新内容添加到旧内容并保存更新的内容你的文件。

//Asumming your values are these:
        List<String> datesList = new ArrayList<String>();
        List<Double> dataList = new ArrayList<Double>();
        //You must fill your data of course...
        //I use a buffer to put in order my data

        StringBuffer stringAppender = new StringBuffer();       
        for (int i = 0; i < dataList.size(); i++) {
            stringAppender.append(dataList.get(i));
            stringAppender.append(",");
            stringAppender.append(datesList.get(i));
            if (i != dataList.size()-1) {
                stringAppender.append("\n");
            }
        }
        //I use the Buffered Writer and then save all the data ordered in one single String.
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("/home/mtataje/saved.txt")));        
        bw.write(stringAppender.toString());
        bw.close();

然后......你有新的输入,对吧?

        //I read my file first
    BufferedReader br = new BufferedReader(new FileReader(new File("/home/mtataje/saved.txt")));
    String line;

    StringBuffer auxBuffer = new StringBuffer();

    while ((line = br.readLine()) != null) {
        auxBuffer.append(line);
        auxBuffer.append("\n");
    }

    //Then append to the StringBuffer again, but your StringBuffer has data saved inside :)
    for (int i = 0; i < newDataListIncoming.size(); i++) {
        auxBuffer.append(newDataListIncoming.get(i));
        auxBuffer.append(",");
        auxBuffer.append(newDatesIncoming.get(i));
        if (i != newDataListIncoming.size()-1) {
            auxBuffer.append("\n");
        }
    }
    //And write your file
    BufferedWriter bw2 = new BufferedWriter(new FileWriter(new File("/home/mtataje/saved.txt")));       
    bw2.write(auxBuffer.toString());
    bw2.close();

当然,你会像我一样在你的代码中使用方法而不是使用冗余,我希望我能帮助你。最好的祝福。

于 2013-05-08T16:43:48.040 回答