有一.csv文件,其中存储为100个人员信息(文件中一行为一个的信息),现在此我想从随机从中读取10个人的信息做处理?请问怎么做?

解决方案 »

  1.   


    随即读取?
    先写一个数据封装的你的人员信息类,我这里只写怎么随即提取10条信息public class Test {
    public static void main(String[] args){
    try{
    FileReader fr = new FileReader("文件路径名");
            BufferedReader br = new BufferedReader(fr);
            Random rand = new Random();
            int[] linenum = new int[10];
            for(int i = 0 ;i < linenum.length ; i++){
                linenum[i] = rand.nextInt(100);
            }
            List list =new ArrayList(Arrays.asList(linenum));  
            StringBuffer bs = new StringBuffer();
            String s = null;
            int i = 0;
            while((s = br.readLine()) != null){
                if(list.contains(i)){
                 bs.append(s);
                 bs.append("\n");
                }
                i++;
            }
            //在将要的10条信息从bs中解析出来 就OK了~,如果你的文件是字节码,还需要通过
            //将数据加工
         }catch(IOException e){
          e.printStackTrace();
         }        }
    }
      

  2.   

    你可以测试下 ,如果很慢的话,就要用RandomAccessFile这类来做了