import java.io.*;
public class  Iotest
{
public void readText(int line_l)throws IOException
{

FileReader input = new FileReader(new File("w://haifei.txt"));
BufferedReader inputLine = new BufferedReader(input); FileWriter output = new FileWriter(new File("w://haixiang.txt"));
BufferedWriter outputLine = new BufferedWriter(output);
String line = inputLine.readLine(); String s = ""; int m = 0; if (m == line_l)
{
s = line;
System.out.println(s);

}

while (line != null)
{
outputLine.write(line,0,line.length()); outputLine.newLine(); line = inputLine.readLine(); }

inputLine.close();
outputLine.close(); } public static void main(String[] args)throws IOException
{
Iotest io = new Iotest();
io.readText(4); }
}"haifei.txt"中的数据如下
1-->JAVA基础
2--->面向对象原理
3--->数组
4--->继承
5--->多台
.
.
.
10-->IO流
读第四行
程序输出   4--->继承高手请看看怎么我写的什么也不输出呀..

解决方案 »

  1.   

    import java.io.*;
    public class  Iotest
    {
    public void readText(int line_l)throws IOException
    {

    FileReader input = new FileReader(new File("w://haifei.txt"));
    BufferedReader inputLine = new BufferedReader(input); String line = inputLine.readLine(); String s = ""; int m = 1; while (line != null)
    {
                                if (m == line_l)
             {
        s = line;
        System.out.println(s);
                                    break;

              }
    line = inputLine.readLine();
                                m++; }

    inputLine.close(); } public static void main(String[] args)throws IOException
    {
    Iotest io = new Iotest();
    io.readText(4); }
    }
      

  2.   

    看看这个:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;public class Iotest
    {  public void readText(int line_l) throws IOException
      {    FileReader input = new FileReader(new File("w://haifei.txt"));
        BufferedReader inputLine = new BufferedReader(input);
        String line;    int count = 0;
        while ((line = inputLine.readLine()) != null) {
          count++;
          if (count == line_l) {
            System.out.println(line);
          }
        }    inputLine.close();
      }  public static void main(String[] args) throws IOException
      {
        Iotest io = new Iotest();
        io.readText(4);
      }
    }