将字符串列表保存到文件中,因为您比较它

时间:2013-05-08 15:54:01

标签: java file bufferedwriter

我将文件保存为双列表(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();
           }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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();

当然,您将使用方法而不是在我的代码中使用冗余,我希望我能帮您解决这个问题。最好的问候。