结构推理 编写一个程序,从键盘输入一串字符,从屏幕输出并将其存入a.txt文件中。
【正确答案】import java.io.*; public class InFile{ public static void main(String[] args) { int ch; try{ FileOutputStream out=new FileOutputStream("a.txt"); while((ch=System.in.read())!='/r'){ System.out.write(ch); out.write(ch); } out.close(); System.out.write('/n'); }catch(IOException e){ System.out.println(e.toString()); } System.out.println("输出至文件完毕!"); } }
【答案解析】