File file = new File("D:/text.txt");

try {

BufferedReader reader = new BufferedReader(new FileReader(file));

String line = null;

while ((line = reader.readLine()) != null) {

System.out.println(line);
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

解决方案 »

  1.   

    我想问下就是我建立的数组,比方a[0].num记录数字,a[0].name记录名字,下一组用a[1]等等,怎么从如图文件中读取并准确放到各个变量中?
      

  2.   

    /**
         * @param path 文件路径
         * @param fgf  分隔符
         * @return
         */
    public static Map<String,String> readFile(String path,String fgf)
        {
            Map<String,String> map = new HashMap<String, String>();
            File file = new File(path);
            BufferedReader reader = null;
            try {
             reader = new BufferedReader(new FileReader(file));
             String tempString = null;
             //一次读入一行,直到读入null为文件结束
             while ((tempString = reader.readLine()) != null) {       
                 String [] tempAry = tempString.split(fgf);
                 map.put(tempAry[0], tempAry[1]);
             }
             reader.close();
            } catch (IOException e) {
             e.printStackTrace();
            } finally {
             if (reader != null) {
              try {
               reader.close();
              } catch (IOException e1) {
              }
             }
            }
               
             return map;
        }readFile("路径"," ");
      

  3.   

    如果lz用java bean类来保存数据的话,建议还是把getter,setter写上。还有,你那个数据没有显示出来,你确定数据设置完之后有repaint。或者你是在初始化的时候加载的数据?