2011-03-06 66 views
0

嗨,所以我正在一個應用程序,目前我有代碼,下載一堆zip文件到/ sdcard /蘭德我已經把所有的文件到一個字符串數組,現在我想要將其中一個zip文件隨機移動到/ sdcard/RAND /使用我的應用程序,然後在應用程序打開時始終使用該文件夾並使用該zip文件。這是爲了保持應用程序的有趣性,因爲每個拉鍊都有不同的做法。感謝任何幫助,然後我可以隨意移動它。Java隨機移動一個文件

package com.android.rand; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 

import android.app.Activity; 
import android.os.Bundle; 

public class Main extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     List<String> w = new ArrayList<String>(); 
     String[] a; 
     File f = new File("/mnt/sdcard/RAND"); 
     for (File s : f.listFiles()) { 
      w.add(f.toString()); 
     } 

     a = new String[w.size()]; 
     a = w.toArray(a); 
    } 

} 

回答

1

既然你有一個字符串數組中的文件名,爲什麼不產生0和長度-1之間的隨機數,將其用作索引到數組,搶文件名和移動。這會起作用嗎?

例如

Random rand = new Random(); 
String path = a[rand.nextInt(a.length)]; 
//note, I used length not length-1 here because of how nextInt works 

//move the file indicated by "path" 
+0

你能不能解釋一下,我怎麼能我很新的文件複製到所有的這些東西,但我確實需要它複製和不動。對不起,我有這樣有限的知識,我正在學習:) – GFlam 2011-03-06 07:07:36