0

I made an android app for learning Java.

Now i like to write objects from a list with objects to a file.

like ...

private List<MyObjects> objects = new ArrayList<MyObjects>();

MyObjects is a extra class and implements "Serializable".

To write the objects in a file i use also a extra class.

Now my problem.

With the below Line i get a FileNotFoundException.

fos = new FileOutputStream(fileName);

When i change this Line with this Line ...

fos = ctx.openFileOutput(fileName, Context.MODE_PRIVATE);

... looks like good but after endig the code i can't find the file in data/data/myPakage/files/.

The file is not exist.

I read the last 4 days a lot of sites and tutorials about that but i can't find my mistake.

Please help my.

I don't need a solved code but a link to the right side or a pointer in my mistaken code is fine.

Sorry for my english. Is not my first language.

I am not sure with code parts you need to get a good overview. If you need more, please tell me.

Here parts of my main site

package com.example.myProject;

import android.os.Bundle;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class ActivityMain extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout_calc);
        TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
    }
}

Here parts of my fragment site

package com.example.myProject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class Fragment_1 extends Fragment implements OnClickListener {
    private Button btnOFF;
    private List<Numbers> number = new ArrayList<Numbers>();
    private View fragment_1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.fragment_1 = inflater.inflate(R.layout.fragment_layout, container, false);

        this.btnOFF = (Button)elementary.findViewById(R.id.id_btnOFF_Elementary);
        this.btnOFF.setOnClickListener(this);

        this.number.add(new Numbers());

        // Here i try to get my data back from the file.
        // Every time i get the information; The file not exist
        // Perhaps the "containsAll" are wrong. But this is in the moment not my problem.
        this.number.containsAll(StreamControl.importNumbers(this.getActivity()));

        return fragment_1;
    }

    @Override
    public void onClick(View buttonView) {

        if (buttonView == this.btnOFF) {

            // Here i try to export data over extra class -- see below
            // I put in my List with objects calls "number" and information
            // for "Context" i hope.
            StreamControl.exportNumbers(number, this.getActivity());
        }
    }
}

Parts of my class Numbers

package com.example.myProject;

import java.io.Serializable;

public class Numbers implements Serializable {

    private static final long serialVersionUID = -5384438724532423282L;


.
.
.
}

Here the code from the file "in" and "out"

package com.example.myProject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;

public class StreamControl {
    public static void exportNumbers(List<Numbers> number, Context ctx) {

        String fileName = "MyCacheFile.ser";
        File cacheFile = new File(fileName);

        FileOutputStream fos = null;
        try {
            // With this line is it not working everytime.
            // File not found exception
            // But to make my own file with "createNewFile()" is not working
            // in data/data i have just read permission.
            fos = new FileOutputStream(cacheFile);

            // If i use this line i have less problems.
            // All Informations "i used Toast on a lot of places" was god.
            // But after it, it was nowhere a file to found.
            //fos = ctx.openFileOutput(fileName, Context.MODE_PRIVATE);

            ObjectOutputStream oos = null;
            try {
                oos = new ObjectOutputStream(fos);
                oos.writeInt(number.size());
                for (int i =0; i < number.size(); i++) {
                oos.writeObject(new Numbers(((Numbers)number.get(i)).getNumbers()));
                }
            }
            catch (IOException e1) { e1.printStackTrace(); }
            finally {
                try {
                    if (oos != null) { oos.close(); }
                }
                catch (IOException ex) { }
            }
        }
        catch (FileNotFoundException e2) { e2.printStackTrace(); }
        finally {
            try {
                if (fos != null) { fos.close(); }
            }
            catch (IOException ex) { ex.printStackTrace(); }
        }
    }

    public static List<Numbers> importNumbers(Context ctx) {

        String fileName = "MyCacheFile.ser";
        int count = 0;
        List<Numbers> number = new ArrayList<Numbers>();

        FileInputStream fis = null;
        try {
            fis = new FileInputStream(fileName);
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(fis);

                count = ois.readInt();
                for (int i = 0; i < count; i++) {
                    number.add(new Numbers(((Numbers) ois.readObject()).getNumbers()));
                }
            }
            catch (IOException ex) { ex.printStackTrace(); }
            catch (ClassNotFoundException ex) { ex.printStackTrace(); }
            finally {
                try {
                    if (ois != null) { ois.close(); }
                }
                catch (IOException ex) { ex.printStackTrace(); }
            }
        }
        catch (FileNotFoundException ex) { ex.printStackTrace(); }
        finally {
            try {
                if (fis != null) { fis.close(); }
            }
            catch (IOException ex) { ex.printStackTrace(); }
        }
        return number;
    }
}

So, i hope that's are enough information.

I looking forward

Benjamin
  • 1
  • 1
  • *looks like good but after endig the code i can't find the file in data/data/myPakage/files/.*. How do you check that directory? – Blackbelt Apr 17 '14 at 12:34
  • I checked it with exists() and i tried to open the file with me inputStream. Both ways told me "File not exist" – Benjamin Apr 17 '14 at 12:38

1 Answers1

0

When you use Context.openFileOutput you create a file in internal storage and you can't check that directory. Take a look at this to save a file to external storage

0xDEADC0DE
  • 2,453
  • 1
  • 17
  • 22
  • that's it, what i read in all the tutorials and on many websites. But how can i read back my data when the file is not exist? I don't like to use the external storage. – Benjamin Apr 17 '14 at 12:44
  • [this](http://stackoverflow.com/questions/7697650/check-if-file-exists-on-sd-card-on-android) question explains how you can check if a file exists in external storage. [this](http://stackoverflow.com/questions/8330276/write-a-file-in-external-storage-in-android) question explains how you can create a new file in external storage – 0xDEADC0DE Apr 17 '14 at 12:50
  • Äh, sorry. I don't have intresting on external storage. Not now.Oh my god! From time to time i need time like this. Now i looked for fis = ctx.openFileInput(fileName); and this is working. Now i can look for the right way to fill my List with the data from the file. Thanks for the attention – Benjamin Apr 17 '14 at 12:59