在文件中保存结果中的变量很多实例 [英] saving in a file results in many instances of variables

查看:174
本文介绍了在文件中保存结果中的变量很多实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的函数来保存一些数据。
该MYDATA是双列表用户输入和dates_Strings是列表(String作为数据类型),它包含字符串格式的日期。

 公共无效savefunc(){        SimpleDateFormat的thedate =新的SimpleDateFormat(DD / MM / YYYY,Locale.US);
        日期D =新的日期();
        字符串formattedDate = thedate.format(D);
        dates_Strings.add(formattedDate);
        双海图= Double.parseDouble(。value.getText()的toString()修剪());
        mydata.add(海图);        文件SD卡= Environment.getExternalStorageDirectory();
        文件目录=新的文件(SD卡,MYFILES);
        directory.mkdirs();
        档案文件=新的文件(目录下,文件名);        FileOutputStream中FOS;
        尝试{
           FOS =新的FileOutputStream(文件,真正的);              BufferedWriter将体重=新的BufferedWriter(新OutputStreamWriter(FOS));
              的for(int i = 0; I< mydata.size();我++){
                    bw.write(mydata.get(I)+,+ dates_Strings.get(ⅰ)+\\ n);
              }
              value.setText();
              bw.flush();
              bw.close();            }赶上(IOException异常E2){
               e2.printStackTrace();
                }//抓住
    }

现在的问题是,在保存时,以下情况:

例如,如果我输入1+保存,2+保存,3+保存+4+ ...保存

在文件中,我可以看到


  

1.0 - > 13年7月5日


  
  

1.0 - > 13年7月5日


  
  

2.0 - > 13年7月5日


  
  

3.0 - > 13年7月5日


  
  

3.0 - > 13年7月5日


  
  

4.0 - > 13年7月5日



解决方案

  FOS =新的FileOutputStream(文件,真正的);

你实例化的FileOutputStream 与附加标志。如果 MYDATA 不会被清零,其教改将在调用文件每次被写入 savefunc

I am using below function to save some data. The mydata is double list that user enter and dates_Strings is list (with String as datatype) which contain dates in string format.

public void savefunc(){

        SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy",Locale.US); 
        Date d=new Date();
        String formattedDate=thedate.format(d);
        dates_Strings.add(formattedDate);


        double thedata=Double.parseDouble(value.getText().toString().trim());
        mydata.add(thedata);

        File sdCard = Environment.getExternalStorageDirectory();
        File directory = new File (sdCard, "MyFiles");
        directory.mkdirs();            
        File file = new File(directory, filename);

        FileOutputStream fos;


        try {
           fos = new FileOutputStream(file,true);

              BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
              for (int i=0;i<mydata.size();i++){
                    bw.write(mydata.get(i)+","+dates_Strings.get(i)+"\n");
              }
              value.setText("");
              bw.flush();
              bw.close();

            } catch (IOException e2) {
               e2.printStackTrace();
                }//catch
    }

Now , the problem is that when saving the following happens:

For example if I enter "1" + save , "2" + save ,"3" +save + "4" +save...

In the file I can see

1.0 -> 07/05/13

1.0 -> 07/05/13

2.0 -> 07/05/13

3.0 -> 07/05/13

3.0 -> 07/05/13

4.0 -> 07/05/13

解决方案

  fos = new FileOutputStream(file,true);

you are instantiating FileOutputStream with append flag. If mydata is never cleared, its cotent will be write in the file everytime you call savefunc

这篇关于在文件中保存结果中的变量很多实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆