我遇到了一个问题:我在/ data / data / files上有一个存储的数据,并且有一个菜单按钮“export”,当我点击它时,文件很好地导出到SDCARD但是大小为0 (文件中没有数据)。 export.class代码:
public Export(Context context,String nom) {
this.context = context;
this.nom=nom;
}
public void transfer(){
try {
File sdCard = Environment.getExternalStorageDirectory();
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
Log.d("Carburant", "Sdcard can read/write !!" );
mExternalStorageAvailable = mExternalStorageWriteable = true;
File dir = new File (sdCard.getAbsolutePath() + "/Carburant/");
dir.mkdirs();
File file = new File(dir, "settings.dat");
//FileOutputStream f = new FileOutputStream(file);
copyfile(nom,file.getAbsolutePath());
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
Log.d("Carburant", "Sdcard only read !!" );
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
}
catch (Exception e) {
Log.d("CARBURANT", e.getMessage());
}
}
private void copyfile(String srFile, String dtFile){
try{
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
Toast.makeText(context, "Export effectué", Toast.LENGTH_SHORT).show();
}
catch(FileNotFoundException ex){
Toast.makeText(context, "File Not found", Toast.LENGTH_SHORT).show();
String x=ex.getMessage();
Log.d("Carburant", x);
}
catch(IOException e){
Toast.makeText(context, "Echec", Toast.LENGTH_SHORT).show();
}
}
}
代码写入文件的位置:
if (data != "" ) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
String fileDir = ""+ preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
myIO.WriteSettings(context,fileDir+fileName, data);
data = "";
WriteSettings方法:
public static void WriteSettings(Context context, String nom, String data) {
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try {
fOut = context.openFileOutput(nom, Context.MODE_APPEND);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
osw.close();
fOut.close();
菜单导出按钮:
case R.id.export:
String mainDirPath = this.getFilesDir() + File.separator + "settings.dat";
FileOutputStream fos;
try {
fos = this.openFileOutput("settings.dat", Context.MODE_PRIVATE);
Export myExport = new Export(this,mainDirPath);
myExport.transfer();
} catch (FileNotFoundException e) {
Log.d("Carburant","File Not found");
}catch(IOException e){
Log.d("Carburant","ERROR");
}
return true;
编辑菜单导出按钮:
case R.id.exporter:
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
fileDir = "" + preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
String mainDirPath = this.getFilesDir() + File.separator + fileDir+fileName;
Log.d("Carburant",mainDirPath);
FileOutputStream fos;
try {
fos = this.openFileOutput(fileDir+fileName, Context.MODE_PRIVATE);
Export myExport = new Export(this,mainDirPath);
myExport.transfer();
} catch (FileNotFoundException e) {
Log.d("Carburant","File Not found");
}catch(IOException e){
Log.d("Carburant","ERROR");
}
return true;
感谢您的帮助。
答案 0 :(得分:2)
也许看看这一行“while((len = in.read(buf))> 0)”
byte[] buffer = new byte[1024];
int len = in.read(buffer);
while (len != -1) {
out.write(buffer, 0, len);
len = in.read(buffer);
}
答案 1 :(得分:1)
我建议在调试器中单步执行此操作,并查看copyfile方法中发生的情况。 src文件'nom'是否存在,大小非零?
祝福,
Phil Lello
答案 2 :(得分:1)
在上面的菜单导出按钮代码中,您正在执行此操作...
case R.id.export:
String mainDirPath = this.getFilesDir() + File.separator + "settings.dat";
这意味着mainDirPath将类似......
/data/data/<package name>/files/settings.dat
问题是当您写入文件...
String fileDir = ""+ preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
myIO.WriteSettings(context,fileDir+fileName, data);
这意味着当你创建mainDirPath时,它应该是......
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String mainDirPath = this.getFilesDir() + File.separator + preferences.getString("login", "") + "." + preferences.getString("marque", "") + "." + "settings.dat";
混乱就是这条线......
fos = this.openFileOutput("settings.dat", Context.MODE_PRIVATE);
...创建一个名为...
的空文件/data/data/<package name>/files/settings.dat
该文件是您正在复制的文件,而不是名为login.marque.settings.dat
的文件。
这有意义吗?