2014-04-17 40 views
0

我制作了一个学习Java的android应用程序。FileOutputStream不能正常工作

现在我喜欢从对象列表中将对象写入文件。

像...

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

MyObjects是一个额外的类,并实现 “可序列化”。

要写入文件中的对象我也使用一个额外的类。

现在我的问题。

随着下面的线我得到一个FileNotFoundException。

fos = new FileOutputStream(fileName); 

当我改变这一行这一行...

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

...看起来不错,但endig代码后,我无法找到数据/数据/ myPakage文件/文件/。

该文件不存在。

我读了最近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; 


. 
. 
. 
} 

这里从文件”部分代码“和”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; 
    } 
} 

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

我期待

+0

*看起来不错,但在endig代码后,我无法在data/data/myPakage/files /.*中找到该文件。你如何检查该目录? – Blackbelt

+0

我检查与存在(),我试图打开与我inputStream的文件。 两种方式告诉我“文件不存在” – Benjamin

回答

0

当您使用Context.openFileOutput创建内部存储上的文件,你不能查该目录。 看看this将文件保存到外部存储器

+0

就是这样,我在所有的教程和许多网站上阅读。 但是,如果文件不存在,我该如何读回数据?我不喜欢使用外部存储。 – Benjamin

+0

[this](http://stackoverflow.com/questions/7697650/check-if-file-exists-on-sd-card-on-android)问题解释了如何检查文件是否存在于外部存储中。 [this](http://stackoverflow.com/questions/8330276/write-a-file-in-external-storage-in-android)问题解释了如何在外部存储器中创建新文件 – 0xDEADC0DE

+0

Äh,抱歉。我对外部存储没有兴趣。不是现在,我的天啊! 时不时我需要这样的时间。 现在我找了 fis = ctx.openFileInput(fileName); 这是工作。 现在我可以寻找正确的方式来填充我的列表与文件中的数据。 感谢您的关注 – Benjamin