0

I am saving to a file a double list (mydata) which is some data the user enters and a string list (dates_Strings) which is the current date.

The user enters some data and pressing a 'save' button , I save the data and the currents date.

So , user may enter "1" and press save (1, 08/05/13)

enter "2" and press save (2, 08/05/13).

Because the user may enter data during a day (same date) I don't want to save many instances of the date.I want to save all the user data in that date.

I tried sth like:

 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");
              }

but it saves only the last entered data.

I am saving as:

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
    }

I am loading as:

 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();
           }
}
George
  • 5,808
  • 15
  • 83
  • 160
  • You'll find your answer in this old thread: http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java – Dek Dekku May 08 '13 at 16:03
  • @DekDekku:thanks but this is another thing.I already append data in the file. – George May 08 '13 at 16:09
  • Is switching to save a binary datastructure an option? – Fildor May 08 '13 at 16:14
  • 1
    Oh, sorry, now i got it. You want to update the entry in the file. I had the same problem and just ended up rewriting the file. What about using a document DB like Redis or Mongo? – Dek Dekku May 08 '13 at 16:19
  • @Fildo:I don't know about binary datastructures ,so I think no. – George May 08 '13 at 16:23
  • @Dek Dekku:I am not familiar with that,I don't know.. – George May 08 '13 at 16:24
  • 1
    A MongoDB course for Java developers is just starting on 10gen website: https://education.10gen.com/courses Perhaps you won't use it on this project, but if you have a few hours a week to spent on it, give it a try. – Dek Dekku May 08 '13 at 16:28

2 Answers2

0

You must load the previous value in your file .. read it and add new value .. then save it !

matzone
  • 5,703
  • 3
  • 17
  • 20
0

Mmmm... maybe this can help you, basic idea as mentioned by our colleagues: receive input, save it in file, receive new input, read the existing file before, add the new content to the old content and save the updated content of your file.

//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();

Then... you have new inputs right?

        //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();

Of course, you will use methods and not use redundancy in your code as me, I hope I gave you a hand with this. Best regards.

Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51
  • :In your example you have a string which contains all the data.I have a string list and a double list. – George May 09 '13 at 09:15
  • Well, my goal was to show you how the process should work, you just needed to adapt it to your requirements; however, I've updated the answer in order to show you how it could be done with your lists (String and Double). Best regards. – Marcelo Tataje May 09 '13 at 13:26
  • :First of all thanks for the help.I updated to show you how I save and how I load.I tried the first code you have and it saves data but it saves "1" and "09/05/13" in the same excel cell.I am saving them in different cells.mydata are in column A and dates are in column B.Aside that , I can't implement your code more.I am not using newDataList.I just use mydata and dates_Strings lists.All the data are written there.I just append the file.So, I have a problem implemented your code. (i am not experiences user) – George May 09 '13 at 14:08
  • well, first of all, you could tell me you're using a file based on cells, I thought you were using just a simple plain text file, but I think you use the result file as a CSV file for excel or something like that (which by the way I imported into excel and showed me the data in A1 and the dates in B1), also, as mentioned, I know you will use only your double and String list since it is a file based app, I just wanted to show you the sequence on how it should work. Hope you have found your solution for this problem. Best regards. – Marcelo Tataje May 09 '13 at 15:07
  • :Ok, I think you are right.I just can't figure how to apply the sequence to all this .Because I only have save and load and you have save ,load ,save.Anyway,thanks! – George May 10 '13 at 08:28