zoukankan      html  css  js  c++  java
  • java 生成文件 保存文件 File FileOutputStream

    // java 生成文件

    public ActionForward createProductPrograma(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) {
       /** **************************权限判断******************************** */
       HttpSession session = request.getSession();
       CheckAuthen checkauthen = new CheckAuthen();
       if (!checkauthen.check(session, 5)) {
        return mapping.findForward("noAuthen");
       }
       /** **************************权限判断******************************** */
       try {
        Properties proper = System.getProperties();
        String sep = proper.getProperty("file.separator");
        proper = null;
        
        String navigationBarPath1 = request.getRealPath("/") + "home" + sep+ "c_product_1.htm";
        String navigationBarPath2 = request.getRealPath("/") + "home" + sep+ "c_product_2.htm";
        String programaContext1 = "我爱河北";
        String programaContext2 = "我爱河北,我爱我家";
        
        File file1 = new File(navigationBarPath1);
        File file2 = new File(navigationBarPath2);
        FileOutputStream out1 = new FileOutputStream(file1); //true为进入追加模式,false为覆盖原有内容
        FileOutputStream out2 = new FileOutputStream(file2); //true为进入追加模式,false为覆盖原有内容

        out1.write(programaContext1.getBytes("utf-8"));
        out2.write(programaContext2.getBytes("utf-8"));

        out1.close();
        out2.close();
       } catch (Exception e) {
        e.printStackTrace();
       }
       return new ActionForward("/home/preview/programa_bar.jsp");
      }

    // java 保存文件

    public ActionForward creatHomeProgramaPart(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws ParseException {
       HttpSession session = request.getSession();
       if(session.getAttribute("managerid")==null){
        return new ActionForward("/manager/sys_manager_login.jsp");
       }
       String area = request.getParameter("area");
       String filecontent =  request.getParameter("content").toString();
       
             if(area!=null){
              try{  
            Properties proper = System.getProperties();
            String sep = proper.getProperty("file.separator");
            proper = null;
            
            String filename = area + ".txt";
            
            if(area.equals("pro_1")){
             filename = "programa_label_1.htm";
            }
            if(area.equals("pro_2")){
             filename = "programa_label_2.htm";
            }
            String filepath = request.getRealPath("/") + "home" + sep +"preview" + sep + filename;
            File file = new File(filepath);
            FileOutputStream out = new FileOutputStream(file); //true为进入追加模式,false为覆盖原有内容
            out.write(filecontent.getBytes("utf-8"));
            out.flush();
            out.close();
             } catch (IOException e) {
           e.printStackTrace();
          }
             }
       request.setAttribute("contextpath", request.getContextPath());  
       return new ActionForward("/productNeworder.do?method=editProductProgramaLabel");
      }

  • 相关阅读:
    C#0004--打开和保存文本文件
    C#0003--如何使用树状视图控件
    C#0002--信息提示框的使用
    C#0001--如何使用错误提醒控件
    使用C#创建简单的联系人备忘录
    SharePoint站点图片轮转器imageRotator
    优秀的代码的设计和组织架构
    思考:为什么每一种开发语言的语法都是不一样的呢(语法设置的不一样的出发点是基于什么考虑)?如果设置成一样有什么不可行的吗?
    分析一个突发问题的思考方法
    思考:架构师的前瞻性能力
  • 原文地址:https://www.cnblogs.com/bailuobo/p/2757397.html
Copyright © 2011-2022 走看看