我製作了一個學習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;
}
}
所以,我希望這是足夠的信息。
我期待
*看起來不錯,但在endig代碼後,我無法在data/data/myPakage/files /.*中找到該文件。你如何檢查該目錄? – Blackbelt
我檢查與存在(),我試圖打開與我inputStream的文件。 兩種方式告訴我「文件不存在」 – Benjamin