Android从资产复制到文件目录

时间:2014-03-24 19:13:46

标签: android

嗨我在启动时在我的assets / data /文件夹中有一个json文件集合我想检查文件是否存在于内部文件目录中,如果它们中的任何一个不存在我想从资产中复制它们/ data /文件夹到内部存储文件目录。目前我正在检测文件是否存在,但很难从资产中复制它们。有谁知道我怎么能做到这一点?

继承人到目前为止我所尝试过的事情

public static void loadData(Context context){

        keyData[] = {"1","2","3","4","5","6"}
    JSONArray exists = new JSONArray();
    Log.v("Data", "keyData = " + keyData);
    Log.v("Data", "keyData.length = " + keyData.length);

        for (String directory : keyData ) {
              File file = new File(directory);
               if(file.exists() && file.isDirectory()){
                   exists.put("true");
               } else {
                   exists.put("false");
               }
        }

    Log.v("Data", "exists = " + exists);
    if(exists.toString().contains("false")){

        for(int i=0; i < keyData.length; i++){
            String filename = keyData[i];
            copyfileFromAssetsToInternalStorage(context,filename);
        }   
    }
} 




 public static void copyfileFromAssetsToInternalStorage(Context context,String filename){

     String DestinationFile = context.getFilesDir().getPath() + File.separator + filename;
     if (!new File(DestinationFile).exists()) {
       try {
         CopyFromAssetsToStorage(context, "data/" + filename, DestinationFile);
       } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
 }  
     private static void CopyFromAssetsToStorage(Context context, String SourceFile, String DestinationFile) throws IOException {
       InputStream IS = context.getAssets().open(SourceFile);
       OutputStream OS = new FileOutputStream(DestinationFile);
       CopyStream(IS, OS);
       OS.flush();
       OS.close();
       IS.close();
     }
     private static void CopyStream(InputStream Input, OutputStream output) throws IOException {
       byte[] buffer = new byte[5120];
       int length = Input.read(buffer);
       while (length > 0) {
         output.write(buffer, 0, length);
         length = Input.read(buffer);
       }
     }


 public final static void writeDataToFile(Context activityContext, String writableString, String fileName){

        FileOutputStream fos=null;
        try {
            fos=activityContext.openFileOutput(fileName, 0);
            activityContext.getFilesDir();
            fos.write(writableString.getBytes());


        } catch (FileNotFoundException e) {
             Log.e("CreateFile", e.getLocalizedMessage());
        }
        catch (IOException e) {
             Log.e("CreateFile", e.getLocalizedMessage());
        }

        finally{
            if(fos!=null){
                try {
                    // drain the stream
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

0 个答案:

没有答案