2014-04-17 46 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