问答详情
源自:2-6 Java 中的 StringBuilder 类的常用方法

编写一个Java Application程序

(1)从命令行后输入任意个参数,把参数用&符号连接成新的字符串,查找新的字符串中是否有gench,如果有,在控制台输出“上海建桥学院”

 (2)将新的字符串写到文件D:\ myfile.txt中;

(3)要求处理该程序中所有可能出现的异常。


提示:

参考StringBuffer类里面的equals、append等方法


提问者:xiaoyu123669 2015-12-03 16:40

个回答

  • HansonQ
    2015-12-03 21:12:26
    已采纳

      BufferedOutputStream bos = null;                                                   
      try {                                                                              
       bos = new BufferedOutputStream(new FileOutputStream(                           
         "D:\\myfile.txt"));                                                    
      } catch (FileNotFoundException e) {                                                
       e.printStackTrace();                                                           
      }                                                                                  
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));          
      System.out.println("请输入一个字符串:");                                                   
      String line;                                                                       
      try {                                                                              
       line = br.readLine();                                                          
       System.out.println("你输入的字符串是:" + line);                                        
       String str = line + "&";                                                       
       if (line.contains("gench")) {                                                  
        System.out.println("上海建桥学院");                                              
       }                                                                              
       bos.write(str.getBytes());                                                     
      } catch (IOException e) {                                                          
       e.printStackTrace();                                                           
      } finally {                                                                        
       try {                                                                          
        bos.close();                                                               
       } catch (IOException e) {                                                      
        e.printStackTrace();                                                       
       }                                                                              
      }                                                                                  
                                                                                               
     }