2011-03-06 67 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