一个文件大致格式如下 
文件名:abcdefg 
   
 13341 英语 english
 13345 汉语 chinese
 13346 西班牙语 span
   .
   .
   .
 99999 火星语 sosese 
   
  要用java读取这个abcdefg文件,并一行一行的按照 空格 分割解析成一个数组(每一行一个数组):  
  比如数组1   str1[]   表示第一行   他的str1[0]=13341   ,str1[1]=英语   str1[2]=english比较简单,但请考虑效率…

解决方案 »

  1.   

    改用XML文件吧.还好解悉,还清楚.
      

  2.   

    建议用xml,后者数据绑定;要不就只能按照楼主的一行一行来了!
      

  3.   

    使用NIO包中的文件映射吧,读出一行之后split,就可以得到楼主需要的东西了。JUST SIMPLE.
      

  4.   

    最后要得到什么结果,两维数组还是嵌套的list
      

  5.   


        public static HashMap<String, String[]> readFile(String path)
        {
            HashMap<String, String[]> map = new HashMap<String, String[]>();
            String[] array;
            FileReader fr = null;
            BufferedReader br = null;        try
            {
                fr = new FileReader(path);
                br = new BufferedReader(fr);
                String str;            while((str = br.readLine()) != null)
                {
                    array = str.split(" ");
                    map.put(array[0], array);
                }
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
                try
                {
                    if(fr != null)
                    {
                        fr.close();
                    }
                    
                    if(br != null)
                    {
                        br.close();
                    }
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
            }
            
            return map;
        }
    呵呵,不晓得这个效率如何,返回HashMap是为了用起方便
      

  6.   


    public List<String[]> getList(){
      List<String[]> list = new ArrayList<String[]>;
      File file = new File("abcdedg");
      FileReader fileReader = new FileReader(file);
      BufferedReader reader = new BufferedReader(fileReader);
      String line = null;
      while((line=reader.readLine())!=null){
        String[] s = line.split(" ");  
        list.add(s); 
      }
      reader.close();
      return list;
    }不知道你要得到什么,给你返回一个list。没有加catch,自己加
      

  7.   

    我也写过一个类似的,用IO实现,效率怎样没测过,
    可是想用xml实现,该怎么做,
    哪位有代码,可否分享一下(xml)
      

  8.   

    写xml里有jdom或者dom4j解析简单~
      

  9.   

    至于返回的类型,我偏向于用二维数组,也可以是List ,这个不是最重要的。假设这个文件足够大,在运行的时候必然会出现问题(跟你应用服务器也有关系),如何处理?
      

  10.   

    如果你真的像提高效率那就太难了,必然说qq的ip数据库文件,读这个文件效率就比较高,但是它不是明文的,要做数据结构分析的,要搞索引,另外我觉得如果简单的处理,效率都相差不多。简单的处理方法你可以自己多测试一下。时间复杂度的比较和i/o的磁盘开销。
      

  11.   


    sunyujia 兄,文件做索引的相关情况可否说的详细点?