FileOutputStream不起作用

时间:2014-04-17 12:26:55

标签: java android

我制作了一个用于学习Java的Android应用程序。

现在,我想将包含对象的列表中的对象写入文件。

喜欢......

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

MyObjects是一个额外的类,实现了“Serializable”。

要在文件中写入对象,我还要使用额外的类。

现在我的问题。

使用下面的行我得到一个FileNotFoundException。

fos = new FileOutputStream(fileName);

当我用这条线改变这条线时......

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

...看起来很不错但在endig代码后我无法在data / data / myPakage / files /中找到该文件。

该文件不存在。

我在过去的4天里阅读了很多网站和教程,但我找不到我的错误。

请帮助我。

我不需要解码的代码,但是我的错误代码中的右侧链接或指针都可以。

抱歉我的英文。不是我的第一语言。

我不确定您需要哪些代码部分才能获得良好的概述。如果您需要更多,请告诉我。

这是我的主要网站的一部分

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

我的片段网站的部分内容

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

我班级的部分号码

package com.example.myProject;

import java.io.Serializable;

public class Numbers implements Serializable {

    private static final long serialVersionUID = -5384438724532423282L;


.
.
.
}

这里是“in”和“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;
    }
}

所以,我希望这是足够的信息。

我期待

1 个答案:

答案 0 :(得分:0)

使用Context.openFileOutput时,您在内部存储中创建了一个文件,但无法检查该目录。 请查看this以将文件保存到外部存储