Java從檔案讀入資料並列印到顯示器

鍾超發表於2011-11-10
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class test {
	public static void main(String[] args) throws IOException {
		File file = new File("nations.txt");
		BufferedReader in = new BufferedReader(new FileReader(file));
		String line = in.readLine();
		while(line != null) {
			StringTokenizer str = new StringTokenizer(line, ",");
			String nation = str.nextToken();
			String capital = str.nextToken();
			System.out.println(nation + "\t\t" + capital);
			line = in.readLine();
		}
		in.close();
	}
}

相關文章