黑马程序员技术交流社区

标题: 有几种方法将字符串存入文件 [打印本页]

作者: zycjiu150    时间: 2016-6-7 23:38
标题: 有几种方法将字符串存入文件
有几种方法将字符串存入文件,并能一次一行保存?
作者: Aaron9527    时间: 2016-6-9 12:00
我的算法是这样的  写入一行敲一次回车保存一次
  1. /*
  2. * 有几种方法将字符串存入文件,并能一次一行保存?
  3. *
  4. * */
  5. public class Demo {
  6.         public static void main(String[] args) throws Exception{       
  7.                 System.out.println("请输入一句话");
  8.                 BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  9.         //        FileOutputStream write = new FileOutputStream("D:\\copy_1.txt");
  10.                 BufferedWriter writee = new BufferedWriter(new FileWriter(new File("D:\\copy_1.txt")));
  11.                
  12.                 while(true){
  13.                         String str = input.readLine();
  14.                         writee.write(str);
  15.                         writee.newLine();
  16.                         if(str=="over"){
  17.                                 break;
  18.                         }
  19.                 }
  20.                 System.out.println("GameOver");
  21.                
  22.         }
  23. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2