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)); } } }