2009年2月17日 星期二

第149題

Place the Fragments into the program, so that the program will get lines from a text file. display them, and
then close all the resources.

Program
import java.io.*;
class ReadFile{
public static void main(String[] args){
try{
File ? = new File("MyText.txt");
Place here ? = new Place here (x1);
Place here x4 = new Place here (x2);
String x3 = null;
while((x3 = ? . Place here ()) != null){
System.out.println(x3);
} ? . Place here ();
}catch(Exception ex){
ex.printStackTrace();
}
}
}


Code Fragments
BufferedReader
StreamReader
FileReader
readLine
readLn
read
closeFile
close
x1

x2
x3

x4


答案:

import java.io.*;
class ReadFile{
public static void main(String[] args){
try{
File x1 = new File("MyText.txt");
FileReader x2 = new FileReader (x1);
BufferedReader x4 = new BufferedReader (x2);
String x3 = null;
while((x3 = x4.readLine ()) != null){
System.out.println(x3);
} x4 . close ();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
參考:10-5-1 BufferedReader與BufferedWriter類別

沒有留言:

張貼留言