我先读入了一个文件
FileReader in=new FileReader("c:/mydir.txt");
这个文本里有很多文字
我要怎样设计才能这样显示出这么文本来
我输入一个"1"它显示一行文件里的文字
这样一直到完全显示完

解决方案 »

  1.   

    public class ReadFileLineByLine {    public static final String FILENAME = "e:/1.txt";    public static void main(String[] args) throws Exception {
            BufferedReader reader = createBufferedReaderFromFile(FILENAME);        try {
                printFileLineByLine(reader);
            } finally {
                reader.close();
            }
        }    private static BufferedReader createBufferedReaderFromFile(String filename)
                throws FileNotFoundException {        return new BufferedReader(new FileReader(filename));
        }    private static void printFileLineByLine(BufferedReader reader) throws IOException {
            String line;        while ((line = readOneLine(reader)) != null) {
                readAndPause(line);
            }        printEndingMessage();
        }    private static void printEndingMessage() {
            System.out.println("\n文件输出完毕。");
        }    private static void readAndPause(String line) {
            printLine(line);
            readUserInput();
        }    private static void readUserInput() {
            Scanner scanner = new Scanner(System.in);
            String line = null;        // 直到用户输入"1"回车才继续
            while (!"1".equals(line)) {
                line = scanner.nextLine();
            }
        }    private static void printLine(String str) {
            System.out.println(str);
        }    private static String readOneLine(BufferedReader reader) throws IOException {
            return reader.readLine();
        }
    }
      

  2.   


    public class Rt {
    public static void main(String[] args) throws IOException {
    try {
    boolean stop=true;
    while(stop){
    BufferedReader br=new BufferedReader(new FileReader("F:\\test.txt"));
    Scanner s=new Scanner(System.in);
    int i=s.nextInt();
    for(int j=0;j<i;j++) {
    System.out.println(br.readLine());
    }
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    }
    }运行结果为:1
    你好
    2
    你好
    haha
    3
    你好
    haha

    4
    你好
    haha

    不dui\
    好了,楼主结贴吧