62,614
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
}
public static String getFileContent(String path) throws IOException {
StringBuffer sb = new StringBuffer();
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(path));
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
} finally {
if (in != null) {
in.close();
in = null;
}
}
return sb.toString();
}
}
public static String getFileContent(String path) throws IOException {
StringBuffer sb = new StringBuffer();
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(path));
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
} finally {
if (in != null) {
in.close();
in = null;
}
}
return sb.toString();
}
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);
}
}
}