2012-03-01 97 views
0

伙计我在我的URL中有一个文本文件。单击一个按钮我可以将它下载到SD卡。将文件从URL替换到原始文件夹

但我需要用原始文件夹中的文件替换下载的文件。两者都是不同的文件。

这是怎么了我是从网址

File sdcard = Environment.getExternalStorageDirectory(); 
File dir = new File (sdcard.getAbsolutePath() + "/varun"); 
dir.mkdirs(); 
try { 
    u = new URL("http://hihowru.com/123.xml"); 
      file = new File(dir,"123.xml"); 
      startTime = System.currentTimeMillis(); 
        Log.d("DownloadManager", "download begining"); 
        Log.d("DownloadManager", "download url:" + url); 
        Log.d("DownloadManager", "downloaded file name:" + "a.mp3"); 
     URLConnection uconnection = u.openConnection(); 
     InputStream is = uconnection.getInputStream(); 
     BufferedInputStream bis = new BufferedInputStream(is); 
        baf = new ByteArrayBuffer(5000); 
         int current = 0; 
         while ((current = bis.read()) != -1) { 
          baf.append((byte) current); 
         } 

       FileOutputStream fos; 

       fos = new FileOutputStream(file); 
       fos.write(baf.toByteArray()); 
       fos.flush(); 
       fos.close(); 

       Toast toast = Toast.makeText(getApplicationContext(), "Downloaded to Sdcard/varun"+audioxml, 0); 
       toast.show(); 
       Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + " sec"); 
       Intent ii = new Intent(DownloadFiles.this,Relaxation.class); 
       startActivity(ii); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

下载,但我需要替换该文件(下载的文件),在原始文件夹中的文件

下载文件名:hi.txt 原始文件夹名称:hw.txt

如何达致这,请帮助

+0

http://stackoverflow.com/questions/5178873/how-to-overwrite-a-text-file-programmatically,有一个职位重写你可以参考这个 – Triode 2012-03-01 11:01:21

回答

0

你不能修改或写入android resourcesasset目录中的文件。由于android apk文件是read only。所以你只能阅读它。最好的方法是将该文件复制到internal storage,然后从该路径使用,也可以从内部存储路径下载url更新后的文件。

更新:

代码拷贝文件从/资产应用程序内部存储。

private void copyFile(String filename) { 
    AssetManager assetManager = this.getAssets(); 

    InputStream in = null; 
    OutputStream out = null; 
    try { 
     in = assetManager.open(filename); 
     String newFileName = "/data/data/" + this.getPackageName() + "/" + filename; 
     out = new FileOutputStream(newFileName); 

     byte[] buffer = new byte[1024]; 
     int read; 
     while ((read = in.read(buffer)) != -1) { 
      out.write(buffer, 0, read); 
     } 
     in.close(); 
     in = null; 
     out.flush(); 
     out.close(); 
     out = null; 
    } catch (Exception e) { 
     Log.e("tag", e.getMessage()); 
    } 

} 
+0

问题的文件,我已经有在应用程序中的默认文件我只需要替换下载的文件 – Goofy 2012-03-01 11:05:12

+0

,以便我可以复制到内部存储数据/数据...但如何更新我的应用程序?请帮忙 – Goofy 2012-03-01 11:09:14

+0

你不能。您必须将该文件复制到其他地方,然后从那里使用它。 – user370305 2012-03-01 11:09:30

相关问题