package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; impo... ...
package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class IO_FirstLetter { public static void main(String[] args) throws IOException { /* * 通過字元輸出流向在項目根目錄的info.txt中寫入以下內容: *黑水朝波咽, *馬上秋郊遠。 *通過字元輸入流讀取info.txt中的所有內容,每次讀取一行,將每一行的第一個文字截取出來並列印在控制臺上。 *PS:控制台列印示例 */ StringBuffer sb=new StringBuffer(); sb.append("黑水朝波咽,"); sb.append("\r\n"); sb.append("馬上秋郊遠。"); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("info.txt"),"gb2312")); bw.write(sb.toString()); bw.flush(); BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("info.txt"),"gb2312")); String str; while((str=br.readLine())!=null) { System.out.println(str.charAt(0)); } } }