日期:2014-05-20  浏览次数:20820 次

如何在读入txt文件行为空的情况下终止循环
Java code

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;


public class Temp
{
    public static void main(String args[]) throws FileNotFoundException
    {
        Scanner in = new Scanner(new File("d:\\test.txt"));
        System.out.println("杨臣刚 - 老鼠爱大米");
        for(int i = 1; i <= 2; i++)//有两行歌词,怎么改写才能终止任意行的循环?
        {    
                String lyric = in.nextLine();
                System.out.println(lyric);
        }
    }
}



------解决方案--------------------
Java code

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;


public class Temp
{
    public static void main(String args[]) throws FileNotFoundException {
        Scanner in = new Scanner(new File("d:\\test.txt"));
        System.out.println("杨臣刚 - 老鼠爱大米");
        String lyric;        
        while(in.hasNextLine) {
              lyric = in.nextLine();
              System.out.println(lyric);
        }
    }
}

------解决方案--------------------
探讨

引用:

引用:
Java code

public static String getFileContent(String path) throws IOException {
StringBuffer sb = new StringBuffer();
BufferedReader in = null;
try {
in ……


正……