http://caterpillar.onlyfun.net/Gossip/JavaGossip-V1/StandardIO.htm
BufferedReader
import java.io.*;
public class Echo {
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("請輸入文字: ");
String text = buf.readLine();
System.out.println("您輸入的文字: " + text);
}
}
Standard input/output public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello! World!");
System.err.println("Error Message Test");
}
}
在command line中的輸出結果(output中僅含有 Hello World!)java HelloWorld >> output.txt Error Message Test○ 標準輸出為 '>', 標準輸入為 '<', 會新增(覆蓋)指定檔案; 使用">>"則會附加於原檔案後面。
讀文件
- 要記得在使用FileReader時,
BufferedReader br = new BufferedReader(new FileReader("dbconfig.txt"));
這樣的話表示dbconfig.txt這個檔案是放在project的根目錄底下!
若要放到其他資料夾(e.g. ref)的話,就要改成FileReader("ref/dbconfig.txt"),以此類推
- 判斷是否文件讀完結束 br.readLine() == null
- 總行數
a. LineNumberReader http://www.kodejava.org/examples/100.html
b. 每readline()一行就count++
沒有留言 :
張貼留言