1

I have a file to which I want to add unique items. The file overwrites each time I try to append it. One class is for adding unique items another is for viewing items as a ListView. NOTE While writing Context.MODE_APPEND while performing write operation, it gives me blank in the ListViewin the second class.

AddingToFile.java

String filename2="grocery24_2.txt";
ArrayList<String> pantryarrlist = new ArrayList<String>();
public void showResult(View v) { button onclick event
    for (Product p : listviewAdapter.getBox()) {
        if (p.selected){
            if(!pantryarrlist.contains(p.name)) {
                pantryarrlist.add(p.name);
                try {
                    FileOutputStream fos = openFileOutput(filename2, Context.MODE_PRIVATE);
// This gives blank->FileOutputStream fos = openFileOutput(filename2, Context.MODE_APPEND);
                    ObjectOutputStream oos = new ObjectOutputStream(fos);
                    oos.writeObject(pantryarrlist);
                    oos.close();
                    fos.close();
                } catch (Exception e) {
                }
            }
        }
    }
}

Pantry.java to read from the file

public class Pantry extends ListActivity{
ListView listView;
ArrayAdapter<String> adapter;
String filename = "grocery24_2.txt";
List newArrList = new ArrayList();
List pantryarrlist = new ArrayList();
File file = new File(filename);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pantry);
    listView = getListView();
    listView.setTextFilterEnabled(true);
    listView.setFocusable(true);

        FileInputStream fis = openFileInput(filename);
        ObjectInputStream ois = new ObjectInputStream(fis);
        newArrList = (ArrayList) ois.readObject();
        pantryarrlist.addAll(newArrList);
        ois.close();
        fis.close();

       adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, newArrList);
       listView.setAdapter(adapter);
    }
Ted
  • 116
  • 10
  • `FileOutputStream fos = openFileOutput(filename2, Context.MODE_PRIVATE);`. Change to appending mode with `FileOutputStream fos = openFileOutput(filename2, Context.MODE_PRIVATE, true);`. Hmmm. That parameter does not exist. Then better use `new FileOutputStream(fullfilepath. true);`. – greenapps May 18 '15 at 10:48
  • it gives me error- openFileOutput() cannot be applied to ContextWrapper – Ted May 18 '15 at 10:53
  • `try { ... } catch (FileNotFoundException e) { } catch (ClassNotFoundException e) { } catch (OptionalDataException e) { } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (Exception e) { }` hehehe you are swallowing all exceptions like old wh** ... fix it first (at least print stacktrace into logcat) without this question is off-topic – Selvin May 18 '15 at 10:53
  • Forget what i said. `FileOutputStream fos = openFileOutput(filename2, Context.MODE_APPEND);` should work. – greenapps May 18 '15 at 10:57
  • @Selvin There are no errors. It just overwrites to the file. For the sake of shortening the code and posting here I removed all the handling of catch statements. – Ted May 18 '15 at 10:58
  • @greenapps As i wrote in the question ..While writing Context.MODE_APPEND while performing write operation, it gives me blank in the ListView in the second class. – Ted May 18 '15 at 10:59
  • Yes i have read that. But have you looked at the file first? Is its size increasing with every save? In your example the `pantryarrlist` is empty so what would it write? Please post a complete save function where you first fill the list with two or three items. So we can see all and try it out. – greenapps May 18 '15 at 11:03
  • For every new p.name you write the pantryarrlist again. Why are you doing that? One time would be enough i would think. – greenapps May 18 '15 at 11:09
  • I think it creates file if not available – Ted May 18 '15 at 11:19
  • @greenapps After writing `FileOutputStream fos = openFileOutput(filename2, Context.MODE_APPEND);` and adding data one at a time by onClick() event...only the first item entered is displayed in `ListView` – Ted May 18 '15 at 11:38
  • Well as already said before: you should check if the file becomes larger and larger first. You should first check if it appends. After that you can look at reading the file. And again: if you want us to really help you you should give code which we can copy and paste to test. – greenapps May 18 '15 at 11:41
  • `For every new p.name you write the pantryarrlist again. Why are you doing that? ` Please answer. – greenapps May 18 '15 at 11:42
  • At writing you write many objects. But at reading you read only one object. So why are you amazed you have only one object? – greenapps May 18 '15 at 11:46
  • it won't be possible to put the code. But if its possible for you then paste `pantryarrlist.add("1");` as dummy code instead of `pantryarrlist.add(p.name);` and run to see what im saying. Then in the `listview` you will see `1`. Now delete it and add `pantryarrlist.add("2");` Still you will see 1 i.e the first item in the `listview` that was added to the file – Ted May 18 '15 at 12:20
  • Don't talk about your listview now. For the third time i ask you to check if the file becomes bigger and bigger while appending. You could also react about the number of reads() you are doing. React to all comments please. And YOU should really post ready to use and ready to copy/paste code to test. Without `listviewAdapter.getBox()` of course. – greenapps May 18 '15 at 13:50

1 Answers1

0

While I'm a bit confused by what you want to achieve, I am going to assume you want multiple pantryarrlist objects in one file. Otherwise you don't need append.

I have created a basic project which writes a String object to a file, then reads it. Using PrintWriter and MODE_APPEND, it goes as expected: text is appended. However, using ObjectOutputStream, MODE_APPEND and writeObject( new String("text") ), it seemed like it wouldn't append and it only reads the first String object. You say in the comments that there is no error, but I can see an exception firing:

java.io.StreamCorruptedException: Wrong format: ac

Can you confirm you have this in your stacktrace as well? I suggest you too create a basic setup where you first write some text, see if it appends, then try to write a simple object and read them all.

The explanation of why this exception happens is here: java.io.StreamCorruptedException: Wrong format when reading more than 1 object

Community
  • 1
  • 1
Jos van Egmond
  • 228
  • 1
  • 8