文本文件中有10条数据,请编写程序读取第4行的信息

解决方案 »

  1.   

    try {
            BufferedReader in = new BufferedReader(new FileReader("F:\\my.txt"));
            String str;
            int row = 0;
            while ((str = in.readLine()) != null) {
                row ++;
                if(row == 4){
                    System.out.println(str);// 这里是第四行
                }
            }
            in.close();
        } catch (IOException e) {
        }
      

  2.   

    楼上说得没错,最好在输出数据后 break; 掉,好像没有必须再继续下去了。
      

  3.   

    public String getLineData(String filePath, int lineNum) {
      BufferedReader in = new BufferedReader(new FileReader(filePath));
      String str = null;
      int count = 0;
      while ((str = in.readLine()) != null) {
          count++;
          if(count == lineNum){
            break;
          }
      }
      in.close();
      
      return str;
    }