2011-04-02 105 views
0

我正在将数据存储到/ data/data/.....中的文件!我想在我的应用程序中添加一个导入导出功能,将SDCARD中的文件备份并导入(并且应用程序会自动读取它)。 我该如何做到这一点? 谢谢。 那是真的吗?导入导出数据文件

public class Import { 
    public static void transfer(){ 
    File sdCard = Environment.getExternalStorageDirectory(); 
    File dir = new File (sdCard.getAbsolutePath() + "/SDCARD/Carburant"); 
    dir.mkdirs(); 
    copyfile("/data/data/carburant.android.com/files/","/SDCARD/Carburant/storeddata"); 

} 

    private static void copyfile(String srFile, String dtFile){ 
     try{ 
      File f1 = new File("/data/data/carburant.android.com/files/"); 
      File f2 = new File("/SDCARD/Carburant/storeddata"); 
      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(); 
      System.out.println("File copied."); 
     } 
     catch(FileNotFoundException ex){ 
      System.out.println(ex.getMessage() + " in the specified directory."); 
      System.exit(0); 
     } 
     catch(IOException e){ 
      System.out.println(e.getMessage());  
     } 
     } 

    } 

回答

1

首先,您需要确保您具有读取/写入数据的正确权限。

然后,你可以得到目录为您的应用程序是这样的:

String myDir = act.getFilesDir(); 

关于Android的安全性和沙盒,你的应用程序将只能够读取/写入此目录,我想。尽管SDCard访问更加开放:

您可以调用Environment.getExternalStorageDirectory()来获取SD卡的根路径并使用它创建FileOutputStream。

一个很好的例子是h ere in stackoverlow

+0

因此,我必须初始化传输操作(所有挂载,读/写执行),然后将文件复制到/ data/data ...中,然后将其复制到SDCARD中的目录中!而已 ? – androniennn 2011-04-02 13:14:43

+0

请看我编辑的第一篇文章。 – androniennn 2011-04-02 13:49:43