当前位置: 代码迷 >> J2SE >> 怎么在读入txt文件行为空的情况下终止循环
  详细解决方案

怎么在读入txt文件行为空的情况下终止循环

热度:100   发布时间:2016-04-24 00:48:25.0
如何在读入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 ……


正……
  相关解决方案