package IOPractice; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; /** * @program: java_project * @description: 字符流实现记事本 * @author: liuliang * @create: 2019-04-21 10:52 **/ public class Practice03 { private static final int BUFFER_SIZE = 1024; /** * @param args */ public static void main(String[] args) { FileWriter fw = null; StringBuilder sb = new StringBuilder(); String str = null; Scanner in = new Scanner(System.in); in.useDelimiter("\n");//设置分隔符----一行以此作为分隔符 System.out.println("请输入,按Enter键换行输入:wq结束输入保存"); while(in.hasNext()){//判断是否继续有输入 str = in.nextLine(); //输入行可以包含空格、空行、且首行也可以输入是空行 if(str.equals(":wq")) { break; } sb.append(str); str = null; } in.close(); System.out.println("输入结束,正在保存..."); try { fw = new FileWriter("E:\\test.txt"); char buf[] = sb.toString().toCharArray(); fw.write(buf, 0, buf.length); System.out.println("保存成功"); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { System.out.println("保存失败"); } } } } }